We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Runtime.Remoting.Activation
- {
-
- using System;
- using System.Security;
- using System.Threading;
- using System.Runtime.InteropServices;
- using System.Runtime.Remoting;
- using System.Runtime.Remoting.Contexts;
- using System.Runtime.Remoting.Proxies;
- using System.Runtime.Remoting.Messaging;
- using System.Runtime.Remoting.Metadata;
- using System.Collections;
- using System.Reflection;
- using System.IO;
- using System.Runtime.Serialization;
- using System.Security.Permissions;
- using System.Globalization;
- using System.Runtime.CompilerServices;
-
-
- static internal class ActivationServices
- {
-
- private static IActivator activator = null;
-
- private static Hashtable _proxyTable = new Hashtable();
- private static Type proxyAttributeType = typeof(System.Runtime.Remoting.Proxies.ProxyAttribute);
- private static ProxyAttribute _proxyAttribute = new ProxyAttribute();
- [ThreadStaticAttribute()]
- static internal ActivationAttributeStack _attributeStack;
-
- internal const string ActivationServiceURI = "RemoteActivationService.rem";
-
- internal const string RemoteActivateKey = "Remote";
- internal const string PermissionKey = "Permission";
- internal const string ConnectKey = "Connect";
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private static void Startup()
- {
- DomainSpecificRemotingData remData = Thread.GetDomain().RemotingData;
-
-
-
- if ((!remData.ActivationInitialized) || remData.InitializingActivation) {
- object configLock = remData.ConfigLock;
- bool fLocked = false;
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- Monitor.ReliableEnter(configLock, ref fLocked);
- remData.InitializingActivation = true;
-
-
- if (!remData.ActivationInitialized) {
-
- BCLDebug.Trace("REMOTE", "Starting up activation service ", Thread.CurrentContext);
-
-
-
-
-
-
- remData.LocalActivator = new LocalActivator();
-
-
-
-
-
-
-
-
-
-
-
- remData.ActivationListener = new ActivationListener();
- remData.ActivationInitialized = true;
-
- }
-
- remData.InitializingActivation = false;
- }
-
- finally {
- if (fLocked) {
- Monitor.Exit(configLock);
- }
- }
- }
- }
-
- private static void InitActivationServices()
- {
-
-
- if (null == activator) {
- activator = GetActivator();
- if (null == activator) {
- Message.DebugOut("Fatal Error... Could not create activator\n");
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_BadInternalState_ActivationFailure")));
- }
- }
- }
-
-
- private static MarshalByRefObject IsCurrentContextOK(Type serverType, object[] props, bool bNewObj)
- {
- BCLDebug.Assert(!serverType.IsInterface, "!serverType.IsInterface");
-
- MarshalByRefObject retObj = null;
-
-
-
-
- InitActivationServices();
-
-
-
- ProxyAttribute pa = GetProxyAttribute(serverType);
- BCLDebug.Assert(null != pa, "null != pa");
-
- if (Object.ReferenceEquals(pa, DefaultProxyAttribute))
- retObj = pa.CreateInstanceInternal(serverType);
- else {
- retObj = pa.CreateInstance(serverType);
-
-
- if (retObj != null) {
-
-
-
- if (!RemotingServices.IsTransparentProxy(retObj) && !serverType.IsAssignableFrom(retObj.GetType())) {
- throw new RemotingException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_Activation_BadObject"), serverType));
- }
- }
- }
- BCLDebug.Assert(null != retObj, "null != retObj");
- return retObj;
- }
-
-
-
-
-
- private static bool IsCurrentContextOK(Type serverType, object[] props, ref ConstructorCallMessage ctorCallMsg)
- {
-
- object[] callSiteAttr = PeekActivationAttributes(serverType);
-
-
- if (callSiteAttr != null) {
- PopActivationAttributes(serverType);
- }
-
- object[] womAttr = new object[1];
- womAttr[0] = GetGlobalAttribute();
-
-
- object[] typeAttr = GetContextAttributesForType(serverType);
-
-
- Context cliCtx = Thread.CurrentContext;
-
-
- ctorCallMsg = new ConstructorCallMessage(callSiteAttr, womAttr, typeAttr, serverType);
-
-
-
-
-
-
-
-
- ctorCallMsg.Activator = new ConstructionLevelActivator();
-
-
-
-
-
-
- bool bCtxOK = QueryAttributesIfContextOK(cliCtx, ctorCallMsg, womAttr);
- if (bCtxOK == true) {
- bCtxOK = QueryAttributesIfContextOK(cliCtx, ctorCallMsg, callSiteAttr);
- if (bCtxOK == true) {
- bCtxOK = QueryAttributesIfContextOK(cliCtx, ctorCallMsg, typeAttr);
- }
- }
-
- return bCtxOK;
- }
-
- private static void CheckForInfrastructurePermission(Assembly asm)
- {
-
-
- if (asm != RemotingServices.s_MscorlibAssembly) {
- CodeAccessSecurityEngine.CheckAssembly(asm, RemotingServices.s_RemotingInfrastructurePermission);
- }
- }
-
- private static bool QueryAttributesIfContextOK(Context ctx, IConstructionCallMessage ctorMsg, object[] attributes)
- {
- bool bCtxOK = true;
- if (attributes != null) {
- for (int i = 0; i < attributes.Length; i++) {
-
-
-
- IContextAttribute attr = attributes[i] as IContextAttribute;
- if (null != attr) {
- Assembly asm = attr.GetType().Assembly;
-
-
- CheckForInfrastructurePermission(asm);
-
- bCtxOK = attr.IsContextOK(ctx, ctorMsg);
- if (bCtxOK == false) {
- break;
- }
- }
- else {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_BadAttribute"));
- }
- }
- }
- return bCtxOK;
- }
-
- static internal void GetPropertiesFromAttributes(IConstructionCallMessage ctorMsg, object[] attributes)
- {
- if (attributes != null) {
- for (int i = 0; i < attributes.Length; i++) {
-
-
-
- IContextAttribute attr = attributes[i] as IContextAttribute;
- if (null != attr) {
- Assembly asm = attr.GetType().Assembly;
-
-
- CheckForInfrastructurePermission(asm);
-
-
-
-
-
-
- attr.GetPropertiesForNewContext(ctorMsg);
- }
- else {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_BadAttribute"));
- }
- }
- }
- }
-
-
- static internal ProxyAttribute DefaultProxyAttribute {
- get { return _proxyAttribute; }
- }
-
-
-
-
- static internal ProxyAttribute GetProxyAttribute(Type serverType)
- {
-
- BCLDebug.Assert(serverType.IsMarshalByRef, "type should be at least marshal-by-ref");
-
- if (!serverType.HasProxyAttribute)
- return DefaultProxyAttribute;
-
-
-
- ProxyAttribute pa = _proxyTable[serverType] as ProxyAttribute;
-
- if (pa == null) {
- object[] ca = Attribute.GetCustomAttributes(serverType, proxyAttributeType, true);
- if ((null != ca) && (0 != ca.Length)) {
- if (!serverType.IsContextful) {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_MBR_ProxyAttribute"));
- }
- pa = ca[0] as ProxyAttribute;
- }
- BCLDebug.Assert(pa != null, "expect proxy attribute");
-
- if (!_proxyTable.Contains(serverType)) {
- lock (_proxyTable) {
- if (!_proxyTable.Contains(serverType)) {
-
- _proxyTable.Add(serverType, pa);
- }
- }
- }
- }
-
- return pa;
- }
-
-
-
- static internal MarshalByRefObject CreateInstance(Type serverType)
- {
-
- MarshalByRefObject retObj = null;
- ConstructorCallMessage ctorCallMsg = null;
- bool bCtxOK = IsCurrentContextOK(serverType, null, ref ctorCallMsg);
- if (bCtxOK && !serverType.IsContextful) {
-
- Message.DebugOut("Allocating blank marshal-by-ref object");
-
-
- retObj = RemotingServices.AllocateUninitializedObject(serverType);
- }
- else {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- RemotingProxy rp;
-
- retObj = (MarshalByRefObject)ConnectIfNecessary(ctorCallMsg);
- if (retObj == null) {
-
-
- Message.DebugOut("Creating remoting proxy for " + serverType.FullName + " in context " + Thread.CurrentContext);
-
- rp = new RemotingProxy(serverType);
-
- Message.DebugOut("Created remoting proxy\n");
- retObj = (MarshalByRefObject)rp.GetTransparentProxy();
- }
- else {
- Message.DebugOut("NewObj did a Connect!");
- rp = (RemotingProxy)RemotingServices.GetRealProxy(retObj);
- }
-
-
- rp.ConstructorMessage = ctorCallMsg;
-
- if (!bCtxOK) {
-
-
- ContextLevelActivator activator = new ContextLevelActivator();
- activator.NextActivator = ctorCallMsg.Activator;
- ctorCallMsg.Activator = activator;
- }
- else {
-
-
-
-
- ctorCallMsg.ActivateInContext = true;
- }
- }
-
- return retObj;
- }
-
-
-
-
-
-
-
-
- static internal IConstructionReturnMessage Activate(RemotingProxy remProxy, IConstructionCallMessage ctorMsg)
- {
- IConstructionReturnMessage ctorRetMsg = null;
-
- if (((ConstructorCallMessage)ctorMsg).ActivateInContext) {
-
- BCLDebug.Assert(ctorMsg.Activator.Level == ActivatorLevel.Construction, "activator level must ActivatorLevel.Construction!");
-
-
- BCLDebug.Assert(ctorMsg.ActivationType.IsContextful, "Inconsistent state during activation");
-
-
- ctorRetMsg = ctorMsg.Activator.Activate(ctorMsg);
-
- if (ctorRetMsg.Exception != null) {
- throw ctorRetMsg.Exception;
- }
- }
- else {
-
- BCLDebug.Assert(ctorMsg.Activator.Level >= ActivatorLevel.Context, "activator level must be at least x-context!");
-
-
-
- BCLDebug.Assert(ActivationServices.CheckIfConnected(remProxy, ctorMsg) == null, "We shouldn't come through this path on a Connect.");
-
-
-
-
-
-
-
-
-
-
-
-
- ActivationServices.GetPropertiesFromAttributes((IConstructionCallMessage)ctorMsg, ctorMsg.CallSiteActivationAttributes);
-
- ActivationServices.GetPropertiesFromAttributes(ctorMsg, ((ConstructorCallMessage)ctorMsg).GetWOMAttributes());
-
- ActivationServices.GetPropertiesFromAttributes((IConstructionCallMessage)ctorMsg, ((ConstructorCallMessage)ctorMsg).GetTypeAttributes());
-
-
- IMessageSink cliCtxChain = Thread.CurrentContext.GetClientContextChain();
-
-
- IMethodReturnMessage retMsg = (IMethodReturnMessage)cliCtxChain.SyncProcessMessage(ctorMsg);
-
- ctorRetMsg = retMsg as IConstructionReturnMessage;
- if (null == retMsg) {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_Failed"));
- }
- else if (retMsg.Exception != null) {
- throw retMsg.Exception;
- }
- }
-
-
- BCLDebug.Assert(null != ctorRetMsg, "Activate returning null ConstructorReturnMessage");
-
- return ctorRetMsg;
- }
-
-
-
-
-
-
-
- static internal IConstructionReturnMessage DoCrossContextActivation(IConstructionCallMessage reqMsg)
- {
- bool bCtxBound = reqMsg.ActivationType.IsContextful;
- Context serverContext = null;
-
- if (bCtxBound) {
-
-
-
-
-
- serverContext = new Context();
-
-
- ArrayList list = (ArrayList)reqMsg.ContextProperties;
- Assembly asm = null;
- for (int i = 0; i < list.Count; i++) {
- IContextProperty prop = list[i] as IContextProperty;
- if (null == prop) {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_BadAttribute"));
- }
- asm = prop.GetType().Assembly;
-
-
- CheckForInfrastructurePermission(asm);
-
-
-
-
- if (serverContext.GetProperty(prop.Name) == null) {
- serverContext.SetProperty(prop);
- }
- }
-
- serverContext.Freeze();
-
-
-
-
- for (int i = 0; i < list.Count; i++) {
- if (!((IContextProperty)list[i]).IsNewContextOK(serverContext)) {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_PropertyUnhappy"));
- }
- }
- }
-
-
- IConstructionReturnMessage replyMsg;
-
- InternalCrossContextDelegate xctxDel = new InternalCrossContextDelegate(DoCrossContextActivationCallback);
-
- object[] args = new object[] {reqMsg};
-
- if (bCtxBound) {
- replyMsg = Thread.CurrentThread.InternalCrossContextCallback(serverContext, xctxDel, args) as IConstructionReturnMessage;
- }
- else {
- replyMsg = xctxDel(args) as IConstructionReturnMessage;
- }
-
- return replyMsg;
- }
-
- static internal object DoCrossContextActivationCallback(object[] args)
- {
- IConstructionCallMessage reqMsg = (IConstructionCallMessage)args[0];
- IConstructionReturnMessage replyMsg = null;
-
-
- IMethodReturnMessage retMsg = (IMethodReturnMessage)Thread.CurrentContext.GetServerContextChain().SyncProcessMessage(reqMsg);
-
- Exception e = null;
- replyMsg = retMsg as IConstructionReturnMessage;
- if (null == replyMsg) {
- if (retMsg != null) {
- e = retMsg.Exception;
- }
- else {
- e = new RemotingException(Environment.GetResourceString("Remoting_Activation_Failed"));
-
- }
- replyMsg = new ConstructorReturnMessage(e, null);
-
-
- ((ConstructorReturnMessage)replyMsg).SetLogicalCallContext((LogicalCallContext)reqMsg.Properties[Message.CallContextKey]);
- }
-
- return replyMsg;
- }
-
- static internal IConstructionReturnMessage DoServerContextActivation(IConstructionCallMessage reqMsg)
- {
- BCLDebug.Assert(reqMsg != null, "NULL ctorReqMsg");
- Exception e = null;
- Type serverType = reqMsg.ActivationType;
- object serverObj = ActivateWithMessage(serverType, reqMsg, null, out e);
-
- IConstructionReturnMessage replyMsg = SetupConstructionReply(serverObj, reqMsg, e);
-
- BCLDebug.Assert(replyMsg != null, "NULL ctorRetMsg");
- return replyMsg;
- }
-
- static internal IConstructionReturnMessage SetupConstructionReply(object serverObj, IConstructionCallMessage ctorMsg, Exception e)
- {
- IConstructionReturnMessage replyMsg = null;
- if (e == null) {
- replyMsg = new ConstructorReturnMessage((MarshalByRefObject)serverObj, null, 0, (LogicalCallContext)ctorMsg.Properties[Message.CallContextKey], ctorMsg);
- }
- else {
- replyMsg = new ConstructorReturnMessage(e, null);
-
-
- ((ConstructorReturnMessage)replyMsg).SetLogicalCallContext((LogicalCallContext)ctorMsg.Properties[Message.CallContextKey]);
-
- }
- return replyMsg;
- }
-
- static internal object ActivateWithMessage(Type serverType, IMessage msg, ServerIdentity srvIdToBind, out Exception e)
- {
- object server = null;
- e = null;
-
-
- server = RemotingServices.AllocateUninitializedObject(serverType);
-
- object proxyForObject = null;
- if (serverType.IsContextful) {
- if (msg is ConstructorCallMessage) {
-
-
-
- proxyForObject = ((ConstructorCallMessage)msg).GetThisPtr();
- }
- else {
-
- proxyForObject = null;
- }
-
-
-
- proxyForObject = RemotingServices.Wrap((ContextBoundObject)server, proxyForObject, false);
- BCLDebug.Assert(RemotingServices.IsTransparentProxy(proxyForObject), "Wrapped object should be a transparent proxy");
- }
- else {
-
-
- if (Thread.CurrentContext != Context.DefaultContext) {
- throw new RemotingException(Environment.GetResourceString("Remoting_Activation_Failed"));
- }
-
- proxyForObject = server;
- }
-
-
- IMessageSink dispatcher = (IMessageSink)new StackBuilderSink(proxyForObject);
-
-
- IMethodReturnMessage retMsg = (IMethodReturnMessage)dispatcher.SyncProcessMessage(msg);
-
- if (retMsg.Exception == null) {
- if (serverType.IsContextful) {
-
- return RemotingServices.Wrap((ContextBoundObject)server);
- }
- else {
- return server;
- }
- }
- else {
- e = retMsg.Exception;
- return null;
- }
- }
-
- static internal void StartListeningForRemoteRequests()
- {
-
- Startup();
- DomainSpecificRemotingData remData = Thread.GetDomain().RemotingData;
- if (!remData.ActivatorListening) {
- object configLock = remData.ConfigLock;
- bool fLocked = false;
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
- Monitor.ReliableEnter(configLock, ref fLocked);
- if (!remData.ActivatorListening) {
- BCLDebug.Log("Registering remoting activator");
-
-
-
-
- RemotingServices.MarshalInternal(Thread.GetDomain().RemotingData.ActivationListener, ActivationServiceURI, typeof(System.Runtime.Remoting.Activation.IActivator));
-
- ServerIdentity srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(ActivationServiceURI);
-
-
- srvID.SetSingletonObjectMode();
-
- remData.ActivatorListening = true;
- }
- }
- finally {
- if (fLocked) {
- Monitor.Exit(configLock);
- }
- }
- }
- }
-
-
- static internal IActivator GetActivator()
- {
- DomainSpecificRemotingData remData = Thread.GetDomain().RemotingData;
- if (remData.LocalActivator == null) {
- Startup();
- }
- return (IActivator)remData.LocalActivator;
- }
-
- static internal void Initialize()
- {
- GetActivator();
- }
-
-
-
- static internal ContextAttribute GetGlobalAttribute()
- {
- DomainSpecificRemotingData remData = Thread.GetDomain().RemotingData;
- if (remData.LocalActivator == null) {
- Startup();
- }
- return (ContextAttribute)remData.LocalActivator;
- }
-
-
-
-
-
-
-
- static internal IContextAttribute[] GetContextAttributesForType(Type serverType)
- {
- if (!(typeof(ContextBoundObject).IsAssignableFrom(serverType)) || serverType.IsCOMObject) {
- return new ContextAttribute[0];
- }
-
- Type currType = serverType;
- object[] currAttr = null;
- int retSize = 8;
- IContextAttribute[] retAttr = new IContextAttribute[retSize];
- int numAttr = 0;
-
-
-
-
- currAttr = currType.GetCustomAttributes(typeof(IContextAttribute), true);
-
- bool bDupe;
- foreach (IContextAttribute attr in currAttr) {
- Type attrType = attr.GetType();
- bDupe = false;
- for (int i = 0; i < numAttr; i++) {
- if (attrType.Equals(retAttr[i].GetType())) {
- bDupe = true;
- break;
- }
- }
-
- if (!bDupe) {
-
- numAttr++;
-
-
-
- if (numAttr > retSize - 1) {
- IContextAttribute[] newAttr = new IContextAttribute