Inheritance: System.MarshalByRefObject, IMessageSink
 public virtual IConstructionReturnMessage Activate(IConstructionCallMessage ctorMsg)
 {
     if (ctorMsg == null)
     {
         throw new ArgumentNullException("ctorMsg");
     }
     if (ctorMsg.Properties.Contains("Remote"))
     {
         return DoRemoteActivation(ctorMsg);
     }
     if (!ctorMsg.Properties.Contains("Permission"))
     {
         return ctorMsg.Activator.Activate(ctorMsg);
     }
     Type activationType = ctorMsg.ActivationType;
     object[] activationAttributes = null;
     if (activationType.IsContextful)
     {
         IList contextProperties = ctorMsg.ContextProperties;
         if ((contextProperties != null) && (contextProperties.Count > 0))
         {
             RemotePropertyHolderAttribute attribute = new RemotePropertyHolderAttribute(contextProperties);
             activationAttributes = new object[] { attribute };
         }
     }
     RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(GetMethodBase(ctorMsg));
     object[] args = Message.CoerceArgs(ctorMsg, reflectionCachedData.Parameters);
     object obj2 = Activator.CreateInstance(activationType, args, activationAttributes);
     if (RemotingServices.IsClientProxy(obj2))
     {
         RedirectionProxy proxy = new RedirectionProxy((MarshalByRefObject) obj2, activationType);
         RemotingServices.MarshalInternal(proxy, null, activationType);
         obj2 = proxy;
     }
     return ActivationServices.SetupConstructionReply(obj2, ctorMsg, null);
 }
Example #2
0
            [System.Security.SecurityCritical]  // auto-generated
            internal ServerIdentity StartupWellKnownObject(
                String asmName, String svrTypeName, String URI, 
                WellKnownObjectMode mode,
                bool fReplace)
            {
                lock (s_wkoStartLock)
                {                
                    MarshalByRefObject obj = null;
                    ServerIdentity srvID = null;

                    // attempt to load the type                
                    Type serverType = LoadType(svrTypeName, asmName);
                
                    // make sure the well known object derives from MarshalByRefObject
                    if(!serverType.IsMarshalByRef)
                    {   
                        throw new RemotingException(
                            Environment.GetResourceString("Remoting_WellKnown_MustBeMBR",
                            svrTypeName));                         
                    }

                    // make sure that no one beat us to creating
                    // the well known object
                    srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                    if ((srvID != null) && srvID.IsRemoteDisconnected())
                    {
                        IdentityHolder.RemoveIdentity(URI);
                        srvID = null;
                    }
                                        
                    if (srvID == null)
                    {                    
                        //WellKnown type instances need to be created under full trust
                        //since the permission set might have been restricted by the channel 
                        //pipeline.           
                        //This assert is protected by Infrastructure link demands.
                        s_fullTrust.Assert();                
                        try {                    
                            obj = (MarshalByRefObject)Activator.CreateInstance(serverType, true);
                                                 
                            if (RemotingServices.IsClientProxy(obj))
                            {
                                // The wellknown type is remoted so we must wrap the proxy
                                // with a local object.

                                // The redirection proxy masquerades as an object of the appropriate
                                // type, and forwards incoming messages to the actual proxy.
                                RedirectionProxy redirectedProxy = new RedirectionProxy(obj, serverType);
                                redirectedProxy.ObjectMode = mode;

                                // DevDiv 720951 and 911924:
                                // 'isInitializing' is propagated into the new ServerIdentity so that other concurrent
                                // operations that find it in URITable do not use it prematurely.
                                RemotingServices.MarshalInternal(redirectedProxy, URI, serverType, updateChannelData: true, isInitializing: true);

                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                                Contract.Assert(null != srvID, "null != srvID");

                                // The redirection proxy handles SingleCall versus Singleton,
                                // so we always set its mode to Singleton.
                                srvID.SetSingletonObjectMode();
                            }
                            else
                            if (serverType.IsCOMObject && (mode == WellKnownObjectMode.Singleton))
                            {
                                // Singleton COM objects are wrapped, so that they will be
                                //   recreated when an RPC server not available is thrown
                                //   if dllhost.exe is killed.
                                ComRedirectionProxy comRedirectedProxy = new ComRedirectionProxy(obj, serverType);

                                // DevDiv 720951 and 911924: isInitializing = true
                                RemotingServices.MarshalInternal(comRedirectedProxy, URI, serverType, updateChannelData: true, isInitializing: true);

                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                                Contract.Assert(null != srvID, "null != srvID");

                                // Only singleton COM objects are redirected this way.
                                srvID.SetSingletonObjectMode();
                            }
                            else
                            {
                                // make sure the object didn't Marshal itself.
                                String tempUri = RemotingServices.GetObjectUri(obj);
                                if (tempUri != null)
                                {
                                    throw new RemotingException(
                                        String.Format(
                                            CultureInfo.CurrentCulture, Environment.GetResourceString(
                                                "Remoting_WellKnown_CtorCantMarshal"),
                                            URI));
                                }

                                // DevDiv 720951 and 911924: isInitializing = true
                                RemotingServices.MarshalInternal(obj, URI, serverType, updateChannelData: true, isInitializing: true);

                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                                Contract.Assert(null != srvID, "null != srvID");

                                if (mode == WellKnownObjectMode.SingleCall)
                                {
                                    // We need to set a special flag in the serverId
                                    // so that every dispatch to this type creates 
                                    // a new instance of the server object
                                    srvID.SetSingleCallObjectMode();
                                }
                                else
                                {
                                    srvID.SetSingletonObjectMode();
                                }
                            }

                        }
                        catch
                        {
                            // DevDiv 720951 and 911924:
                            // An exception thrown in the scope of this attempt to create a
                            // new ServerIdentity may leave a ServerIdentity instance in the
                            // URITable that has not been properly initialized.  This condition
                            // has been true for all versions of the framework but was first
                            // recognized in making this fix in 4.5.3.  Ideally, a damaged
                            // ServerIdentity should be removed from URITable.  But due to risk
                            // and lack of customer reports of a problem, we chose not to fix it.
                            throw;
                        }
                        finally {

                            // DevDiv 720951 and 911924:
                            // This flag is cleared only after the new ServerIdentity is completely
                            // initialized and ready for use.  It is done here in the 'finally' to
                            // ensure all exit paths reset the flag, including exceptions.
                            if (srvID != null)
                            {
                                srvID.IsInitializing = false;
                            }

                            SecurityPermission.RevertAssert();
                        }
                    }
                    
                    Contract.Assert(null != srvID, "null != srvID");
                    return srvID;
                }
            } // StartupWellKnownObject
        public virtual IConstructionReturnMessage Activate(
            IConstructionCallMessage ctorMsg)
        {
            // This is where the activation service hooks in to activation
            // requests. We get called as the activation message is recognized
            // by the ClientContextTerminatorSink & routed to us. 
            //
            // NOTE: This gets called for both purely within appDomain activation
            // and 'real' remote activation scenarios as the request goes out of
            // the client context.
            // It also gets called as an incoming remote call is routed to the
            // local activator by the remote activator object.
            //
            BCLDebug.Log("Activation Services:: new Activate()");
            if (ctorMsg == null)
            {
                throw new ArgumentNullException("ctorMsg");
            }
            Contract.EndContractBlock();
            
            // Check if we have marked this activation to go remote 
            if (ctorMsg.Properties.Contains(ActivationServices.RemoteActivateKey))
            {
                //DBG Console.WriteLine("Attempting remote activation!");
                                
                return DoRemoteActivation(ctorMsg);
            }
            else
            {                
                // We must be in either a pure cross context activation or
                // a remote incoming activation request (in which case we
                // already checked the permission to create an instance of
                // this type).
                if (ctorMsg.Properties.Contains(ActivationServices.PermissionKey))
                { 
                    Type activationType = ctorMsg.ActivationType;                    

                    // We are on the server end of a real remote activation
                    // Create a local attribute that contributes the context
                    // properties requested by the remote request
                    Object[] attr = null;
                    if (activationType.IsContextful)
                    {
                    IList cp = ctorMsg.ContextProperties;
                    if (cp != null && cp.Count > 0)
                    {
                        RemotePropertyHolderAttribute rph = new RemotePropertyHolderAttribute(cp);
                        attr = new Object[1];
                        attr[0] = rph;
                    }
                    }
                    MethodBase mb = GetMethodBase(ctorMsg); 
                    RemotingMethodCachedData methodCache = 
                                            InternalRemotingServices.GetReflectionCachedData(mb);
                    Object[] args = Message.CoerceArgs(ctorMsg, methodCache.Parameters);
                    
                    Object server = Activator.CreateInstance(
                        activationType, 
                        args,
                        attr);

                    // check to see if we need to do redirection
                    if (RemotingServices.IsClientProxy(server))
                    {
                        // The wellknown type is remoted so we must wrap the proxy
                        // with a local object.

                        // The redirection proxy masquerades as an object of the appropriate
                        // type, and forwards incoming messages to the actual proxy.
                        RedirectionProxy redirectedProxy = 
                            new RedirectionProxy((MarshalByRefObject)server, activationType);
                        RemotingServices.MarshalInternal(redirectedProxy, null, activationType);

                        server = redirectedProxy;
                    }                        
                     
                    return ActivationServices.SetupConstructionReply(
                        server, ctorMsg, null);
                }
                else
                {
                    BCLDebug.Log("Attempting X-Context activation!");
                    // delegate to the Activator in the message 
                    return ctorMsg.Activator.Activate(ctorMsg);
                }
            }
        }
Example #4
0
            [System.Security.SecurityCritical]  // auto-generated
            internal ServerIdentity StartupWellKnownObject( 
                String asmName, String svrTypeName, String URI, 
                WellKnownObjectMode mode,
                bool fReplace) 
            {
                lock (s_wkoStartLock)
                {
                    MarshalByRefObject obj = null; 
                    ServerIdentity srvID = null;
 
                    // attempt to load the type 
                    Type serverType = LoadType(svrTypeName, asmName);
 
                    // make sure the well known object derives from MarshalByRefObject
                    if(!serverType.IsMarshalByRef)
                    {
                        throw new RemotingException( 
                            Environment.GetResourceString("Remoting_WellKnown_MustBeMBR",
                            svrTypeName)); 
                    } 

                    // make sure that no one beat us to creating 
                    // the well known object
                    srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                    if ((srvID != null) && srvID.IsRemoteDisconnected())
                    { 
                        IdentityHolder.RemoveIdentity(URI);
                        srvID = null; 
                    } 

                    if (srvID == null) 
                    {
                        //WellKnown type instances need to be created under full trust
                        //since the permission set might have been restricted by the channel
                        //pipeline. 
                        //This assert is protected by Infrastructure link demands.
                        s_fullTrust.Assert(); 
                        try { 
                            obj = (MarshalByRefObject)Activator.CreateInstance(serverType, true);
 
                            if (RemotingServices.IsClientProxy(obj))
                            {
                                // The wellknown type is remoted so we must wrap the proxy
                                // with a local object. 

                                // The redirection proxy masquerades as an object of the appropriate 
                                // type, and forwards incoming messages to the actual proxy. 
                                RedirectionProxy redirectedProxy = new RedirectionProxy(obj, serverType);
                                redirectedProxy.ObjectMode = mode; 
                                RemotingServices.MarshalInternal(redirectedProxy, URI, serverType);

                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                                Contract.Assert(null != srvID, "null != srvID"); 

                                // The redirection proxy handles SingleCall versus Singleton, 
                                // so we always set its mode to Singleton. 
                                srvID.SetSingletonObjectMode();
                            } 
                            else
                            if (serverType.IsCOMObject && (mode == WellKnownObjectMode.Singleton))
                            {
                                // Singleton COM objects are wrapped, so that they will be 
                                //   recreated when an RPC server not available is thrown
                                //   if dllhost.exe is killed. 
                                ComRedirectionProxy comRedirectedProxy = new ComRedirectionProxy(obj, serverType); 
                                RemotingServices.MarshalInternal(comRedirectedProxy, URI, serverType);
 
                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                                Contract.Assert(null != srvID, "null != srvID");

                                // Only singleton COM objects are redirected this way. 
                                srvID.SetSingletonObjectMode();
                            } 
                            else 
                            {
                                // make sure the object didn't Marshal itself. 
                                String tempUri = RemotingServices.GetObjectUri(obj);
                                if (tempUri != null)
                                {
                                    throw new RemotingException( 
                                        String.Format(
                                            CultureInfo.CurrentCulture, Environment.GetResourceString( 
                                                "Remoting_WellKnown_CtorCantMarshal"), 
                                            URI));
                                } 

                                RemotingServices.MarshalInternal(obj, URI, serverType);

                                srvID = (ServerIdentity)IdentityHolder.ResolveIdentity(URI); 
                                Contract.Assert(null != srvID, "null != srvID");
 
                                if (mode == WellKnownObjectMode.SingleCall) 
                                {
                                    // We need to set a special flag in the serverId 
                                    // so that every dispatch to this type creates
                                    // a new instance of the server object
                                    srvID.SetSingleCallObjectMode();
                                } 
                                else
                                { 
                                    srvID.SetSingletonObjectMode(); 
                                }
                            } 
                        }
                        catch
                        {
                            throw; 
                        }
                        finally { 
                            SecurityPermission.RevertAssert(); 
                        }
                    } 

                    Contract.Assert(null != srvID, "null != srvID");
                    return srvID;
                } 
            } // StartupWellKnownObject
 internal ServerIdentity StartupWellKnownObject(string asmName, string svrTypeName, string URI, WellKnownObjectMode mode, bool fReplace)
 {
     lock (s_wkoStartLock)
     {
         MarshalByRefObject obj2 = null;
         ServerIdentity identity = null;
         Type type = LoadType(svrTypeName, asmName);
         if (!type.IsMarshalByRef)
         {
             throw new RemotingException(Environment.GetResourceString("Remoting_WellKnown_MustBeMBR", new object[] { svrTypeName }));
         }
         identity = (ServerIdentity) IdentityHolder.ResolveIdentity(URI);
         if ((identity != null) && identity.IsRemoteDisconnected())
         {
             IdentityHolder.RemoveIdentity(URI);
             identity = null;
         }
         if (identity == null)
         {
             s_fullTrust.Assert();
             try
             {
                 obj2 = (MarshalByRefObject) Activator.CreateInstance(type, true);
                 if (RemotingServices.IsClientProxy(obj2))
                 {
                     RedirectionProxy proxy = new RedirectionProxy(obj2, type) {
                         ObjectMode = mode
                     };
                     RemotingServices.MarshalInternal(proxy, URI, type);
                     identity = (ServerIdentity) IdentityHolder.ResolveIdentity(URI);
                     identity.SetSingletonObjectMode();
                 }
                 else if (type.IsCOMObject && (mode == WellKnownObjectMode.Singleton))
                 {
                     ComRedirectionProxy proxy2 = new ComRedirectionProxy(obj2, type);
                     RemotingServices.MarshalInternal(proxy2, URI, type);
                     identity = (ServerIdentity) IdentityHolder.ResolveIdentity(URI);
                     identity.SetSingletonObjectMode();
                 }
                 else
                 {
                     if (RemotingServices.GetObjectUri(obj2) != null)
                     {
                         throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CtorCantMarshal"), new object[] { URI }));
                     }
                     RemotingServices.MarshalInternal(obj2, URI, type);
                     identity = (ServerIdentity) IdentityHolder.ResolveIdentity(URI);
                     if (mode == WellKnownObjectMode.SingleCall)
                     {
                         identity.SetSingleCallObjectMode();
                     }
                     else
                     {
                         identity.SetSingletonObjectMode();
                     }
                 }
             }
             catch
             {
                 throw;
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
         }
         return identity;
     }
 }
 internal ServerIdentity StartupWellKnownObject(string asmName, string svrTypeName, string URI, WellKnownObjectMode mode, bool fReplace)
 {
     lock (s_wkoStartLock)
     {
         MarshalByRefObject obj2     = null;
         ServerIdentity     identity = null;
         Type type = LoadType(svrTypeName, asmName);
         if (!type.IsMarshalByRef)
         {
             throw new RemotingException(Environment.GetResourceString("Remoting_WellKnown_MustBeMBR", new object[] { svrTypeName }));
         }
         identity = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
         if ((identity != null) && identity.IsRemoteDisconnected())
         {
             IdentityHolder.RemoveIdentity(URI);
             identity = null;
         }
         if (identity == null)
         {
             s_fullTrust.Assert();
             try
             {
                 obj2 = (MarshalByRefObject)Activator.CreateInstance(type, true);
                 if (RemotingServices.IsClientProxy(obj2))
                 {
                     RedirectionProxy proxy = new RedirectionProxy(obj2, type)
                     {
                         ObjectMode = mode
                     };
                     RemotingServices.MarshalInternal(proxy, URI, type);
                     identity = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                     identity.SetSingletonObjectMode();
                 }
                 else if (type.IsCOMObject && (mode == WellKnownObjectMode.Singleton))
                 {
                     ComRedirectionProxy proxy2 = new ComRedirectionProxy(obj2, type);
                     RemotingServices.MarshalInternal(proxy2, URI, type);
                     identity = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                     identity.SetSingletonObjectMode();
                 }
                 else
                 {
                     if (RemotingServices.GetObjectUri(obj2) != null)
                     {
                         throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CtorCantMarshal"), new object[] { URI }));
                     }
                     RemotingServices.MarshalInternal(obj2, URI, type);
                     identity = (ServerIdentity)IdentityHolder.ResolveIdentity(URI);
                     if (mode == WellKnownObjectMode.SingleCall)
                     {
                         identity.SetSingleCallObjectMode();
                     }
                     else
                     {
                         identity.SetSingletonObjectMode();
                     }
                 }
             }
             catch
             {
                 throw;
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
         }
         return(identity);
     }
 }