Ejemplo n.º 1
0
        internal static bool RemoveDynamicProperty(MarshalByRefObject obj, String name)
        {
            if (RemotingServices.IsObjectOutOfContext(obj))
            {
                // We have to add a proxy side property, get the identity
                RealProxy rp = RemotingServices.GetRealProxy(obj);
                return(rp.IdentityObject.RemoveProxySideDynamicProperty(name));
            }
            else
            {
                MarshalByRefObject realObj =
                    (MarshalByRefObject)
                    RemotingServices.AlwaysUnwrap((ContextBoundObject)obj);

                // This is a real object. See if we have an identity for it
                ServerIdentity srvID = (ServerIdentity)MarshalByRefObject.GetIdentity(realObj);
                if (srvID != null)
                {
                    return(srvID.RemoveServerSideDynamicProperty(name));
                }
                else
                {
                    // identity not found, we can't set a sink for this object.
                    throw new RemotingException(
                              Environment.GetResourceString("Remoting_NoIdentityEntry"));
                }
            }
        }
Ejemplo n.º 2
0
 internal RedirectionProxy(MarshalByRefObject proxy, Type serverType)
 {
     _proxy      = proxy;
     _realProxy  = RemotingServices.GetRealProxy(_proxy);
     _serverType = serverType;
     _objectMode = WellKnownObjectMode.Singleton;
 } // RedirectionProxy
Ejemplo n.º 3
0
 internal RedirectionProxy(MarshalByRefObject proxy, Type serverType)
 {
     this._proxy      = proxy;
     this._realProxy  = RemotingServices.GetRealProxy((object)this._proxy);
     this._serverType = serverType;
     this._objectMode = WellKnownObjectMode.Singleton;
 }
Ejemplo n.º 4
0
        } // ObjectMode

        public virtual IMessage SyncProcessMessage(IMessage msg)
        {
            IMessage replyMsg = null;

            try
            {
                msg.Properties["__Uri"] = _realProxy.IdentityObject.URI;

                if (_objectMode == WellKnownObjectMode.Singleton)
                {
                    replyMsg = _realProxy.Invoke(msg);
                }
                else
                {
                    // This is a single call object, so we need to create
                    // a new instance.
                    MarshalByRefObject obj = (MarshalByRefObject)Activator.CreateInstance(_serverType, true);
                    BCLDebug.Assert(RemotingServices.IsTransparentProxy(obj), "expecting a proxy");

                    RealProxy rp = RemotingServices.GetRealProxy(obj);
                    replyMsg = rp.Invoke(msg);
                }
            }
            catch (Exception e)
            {
                replyMsg = new ReturnMessage(e, msg as IMethodCallMessage);
            }

            return(replyMsg);
        } // SyncProcessMessage
        internal static ServerIdentity FindOrCreateServerIdentity(MarshalByRefObject obj, string objURI, int flags)
        {
            ServerIdentity idObj = null;
            bool           flag;

            idObj = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out flag);
            if (idObj == null)
            {
                Context serverCtx = null;
                if (obj is ContextBoundObject)
                {
                    serverCtx = Thread.CurrentContext;
                }
                else
                {
                    serverCtx = DefaultContext;
                }
                ServerIdentity id = new ServerIdentity(obj, serverCtx);
                if (flag)
                {
                    idObj = obj.__RaceSetServerIdentity(id);
                }
                else
                {
                    RealProxy realProxy = null;
                    realProxy = RemotingServices.GetRealProxy(obj);
                    realProxy.IdentityObject = id;
                    idObj = (ServerIdentity)realProxy.IdentityObject;
                }
            }
            if (IdOps.bStrongIdentity(flags))
            {
                ReaderWriterLock tableLock = TableLock;
                bool             flag2     = !tableLock.IsWriterLockHeld;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (flag2)
                    {
                        tableLock.AcquireWriterLock(0x7fffffff);
                    }
                    if ((idObj.ObjURI == null) || !idObj.IsInIDTable())
                    {
                        SetIdentity(idObj, objURI, DuplicateIdentityOption.Unique);
                    }
                    if (idObj.IsDisconnected())
                    {
                        idObj.SetFullyConnected();
                    }
                }
                finally
                {
                    if (flag2 && tableLock.IsWriterLockHeld)
                    {
                        tableLock.ReleaseWriterLock();
                    }
                }
            }
            return(idObj);
        }
Ejemplo n.º 6
0
        public void AttachServerObject(MarshalByRefObject serverObject, Context context)
        {
            DisposeServerObject();

            _context      = context;
            _serverObject = serverObject;

            if (RemotingServices.IsTransparentProxy(serverObject))
            {
                RealProxy rp = RemotingServices.GetRealProxy(serverObject);
                if (rp.ObjectIdentity == null)
                {
                    rp.ObjectIdentity = this;
                }
            }
            else
            {
                if (_objectType.IsContextful)
                {
                    _envoySink = context.CreateEnvoySink(serverObject);
                }

                _serverObject.ObjectIdentity = this;
            }
        }
Ejemplo n.º 7
0
        internal static ServerIdentity FindOrCreateServerIdentity(MarshalByRefObject obj, string objURI, int flags)
        {
            bool           fServer;
            ServerIdentity serverIdentity1 = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

            if (serverIdentity1 == null)
            {
                Context        serverCtx = !(obj is ContextBoundObject) ? IdentityHolder.DefaultContext : Thread.CurrentContext;
                ServerIdentity id        = new ServerIdentity(obj, serverCtx);
                if (fServer)
                {
                    serverIdentity1 = obj.__RaceSetServerIdentity(id);
                }
                else
                {
                    RealProxy      realProxy       = RemotingServices.GetRealProxy((object)obj);
                    ServerIdentity serverIdentity2 = id;
                    realProxy.IdentityObject = (Identity)serverIdentity2;
                    serverIdentity1          = (ServerIdentity)realProxy.IdentityObject;
                }
                if (IdOps.bIsInitializing(flags))
                {
                    serverIdentity1.IsInitializing = true;
                }
            }
            if (IdOps.bStrongIdentity(flags))
            {
                ReaderWriterLock tableLock = IdentityHolder.TableLock;
                bool             flag      = !tableLock.IsWriterLockHeld;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (flag)
                    {
                        tableLock.AcquireWriterLock(int.MaxValue);
                    }
                    if (serverIdentity1.ObjURI == null || !serverIdentity1.IsInIDTable())
                    {
                        IdentityHolder.SetIdentity((Identity)serverIdentity1, objURI, DuplicateIdentityOption.Unique);
                    }
                    if (serverIdentity1.IsDisconnected())
                    {
                        serverIdentity1.SetFullyConnected();
                    }
                }
                finally
                {
                    if (flag && tableLock.IsWriterLockHeld)
                    {
                        tableLock.ReleaseWriterLock();
                    }
                }
            }
            return(serverIdentity1);
        }
Ejemplo n.º 8
0
 // Determine if an object is outside the current context.
 public static bool IsObjectOutOfContext(Object tp)
 {
     if (IsTransparentProxy(tp))
     {
         RealProxy proxy = GetRealProxy(tp);
         Identity  id    = proxy.Identity;
         if (id != null && id.context != Thread.CurrentContext)
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 9
0
 public static IMethodReturnMessage ExecuteMessage(
     MarshalByRefObject target, IMethodCallMessage reqMsg)
 {
     if (IsTransparentProxy(target))
     {
         // Message must go through all chain of sinks
         RealProxy rp = GetRealProxy(target);
         return((IMethodReturnMessage)rp.Invoke(reqMsg));
     }
     else                // Direct call
     {
         return(InternalExecuteMessage(target, reqMsg));
     }
 }
Ejemplo n.º 10
0
 // Get the envoy chain for a specific proxy object.
 public static IMessageSink GetEnvoyChainForProxy
     (MarshalByRefObject obj)
 {
     if (IsObjectOutOfContext(obj))
     {
         RealProxy proxy = GetRealProxy(obj);
         Identity  id    = proxy.Identity;
         if (id != null)
         {
             return(id.envoyChain);
         }
     }
     return(null);
 }
Ejemplo n.º 11
0
        public static void SetObjectUriForMarshal(MarshalByRefObject obj, string uri)
        {
            if (IsTransparentProxy(obj))
            {
                RealProxy proxy    = RemotingServices.GetRealProxy(obj);
                Identity  identity = proxy.ObjectIdentity;

                if (identity != null && !(identity is ServerIdentity) && !proxy.GetProxiedType().IsContextful)
                {
                    throw new RemotingException("SetObjectUriForMarshal method should only be called for MarshalByRefObjects that exist in the current AppDomain.");
                }
            }

            Marshal(obj, uri);
        }
        internal static bool RemoveDynamicProperty(MarshalByRefObject obj, string name)
        {
            if (RemotingServices.IsObjectOutOfContext(obj))
            {
                RealProxy realProxy = RemotingServices.GetRealProxy(obj);
                return(realProxy.IdentityObject.RemoveProxySideDynamicProperty(name));
            }
            MarshalByRefObject obj2           = (MarshalByRefObject)RemotingServices.AlwaysUnwrap((ContextBoundObject)obj);
            ServerIdentity     serverIdentity = (ServerIdentity)MarshalByRefObject.GetIdentity(obj2);

            if (serverIdentity != null)
            {
                return(serverIdentity.RemoveServerSideDynamicProperty(name));
            }
            throw new RemotingException(Environment.GetResourceString("Remoting_NoIdentityEntry"));
        }
 internal ServerIdentity(MarshalByRefObject obj, Context serverCtx) : base(obj is ContextBoundObject)
 {
     if (obj != null)
     {
         if (!RemotingServices.IsTransparentProxy(obj))
         {
             this._srvType = obj.GetType();
         }
         else
         {
             RealProxy realProxy = RemotingServices.GetRealProxy(obj);
             this._srvType = realProxy.GetProxiedType();
         }
     }
     this._srvCtx            = serverCtx;
     this._serverObjectChain = null;
     this._stackBuilderSink  = null;
 }
Ejemplo n.º 14
0
        public static bool Disconnect(MarshalByRefObject obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            ServerIdentity identity;

            if (IsTransparentProxy(obj))
            {
                // CBOs are always accessed through a proxy, even in the server, so
                // for server CBOs it is ok to disconnect a proxy

                RealProxy proxy = GetRealProxy(obj);
                if (proxy.GetProxiedType().IsContextful&& (proxy.ObjectIdentity is ServerIdentity))
                {
                    identity = proxy.ObjectIdentity as ServerIdentity;
                }
                else
                {
                    throw new ArgumentException("The obj parameter is a proxy.");
                }
            }
            else
            {
                identity           = obj.ObjectIdentity;
                obj.ObjectIdentity = null;
            }

            if (identity == null || !identity.IsConnected)
            {
                return(false);
            }
            else
            {
                LifetimeServices.StopTrackingLifetime(identity);
                DisposeIdentity(identity);
                TrackingServices.NotifyDisconnectedObject(obj);
                return(true);
            }
        }
Ejemplo n.º 15
0
 // Get an object reference that represents an object proxy.
 public static ObjRef GetObjRefForProxy(MarshalByRefObject obj)
 {
     if (IsTransparentProxy(obj))
     {
         RealProxy proxy = GetRealProxy(obj);
         Identity  id    = proxy.Identity;
         if (id != null)
         {
             return(id.objRef);
         }
         else
         {
             return(null);
         }
     }
     else
     {
         throw new RemotingException(_("Remoting_NoProxy"));
     }
 }
Ejemplo n.º 16
0
        //   Creates a new server identity. This form is used by RemotingServices.Wrap
        //
        internal ServerIdentity(MarshalByRefObject obj, Context serverCtx) : base(obj is ContextBoundObject)
        {
            if (null != obj)
            {
                if (!RemotingServices.IsTransparentProxy(obj))
                {
                    _srvType = obj.GetType();
                }
                else
                {
                    RealProxy rp = RemotingServices.GetRealProxy(obj);
                    _srvType = rp.GetProxiedType();
                }
            }

            _srvCtx            = serverCtx;
            _serverObjectChain = null;
            _stackBuilderSink  = null;
            _refCount          = 0;
        }
Ejemplo n.º 17
0
        public virtual IMessage SyncProcessMessage(IMessage msg)
        {
            IMessage result = null;

            try
            {
                msg.Properties["__Uri"] = this._realProxy.IdentityObject.URI;
                if (this._objectMode == WellKnownObjectMode.Singleton)
                {
                    result = this._realProxy.Invoke(msg);
                }
                else
                {
                    MarshalByRefObject proxy     = (MarshalByRefObject)Activator.CreateInstance(this._serverType, true);
                    RealProxy          realProxy = RemotingServices.GetRealProxy(proxy);
                    result = realProxy.Invoke(msg);
                }
            }
            catch (Exception e)
            {
                result = new ReturnMessage(e, msg as IMethodCallMessage);
            }
            return(result);
        }
Ejemplo n.º 18
0
        public static ObjRef Marshal(MarshalByRefObject Obj, string ObjURI, Type RequestedType)
        {
            if (IsTransparentProxy(Obj))
            {
                RealProxy proxy    = RemotingServices.GetRealProxy(Obj);
                Identity  identity = proxy.ObjectIdentity;

                if (identity != null)
                {
                    if (proxy.GetProxiedType().IsContextful&& !identity.IsConnected)
                    {
                        // Unregistered local contextbound object. Register now.
                        ClientActivatedIdentity cboundIdentity = (ClientActivatedIdentity)identity;
                        if (ObjURI == null)
                        {
                            ObjURI = NewUri();
                        }
                        cboundIdentity.ObjectUri = ObjURI;
                        RegisterServerIdentity(cboundIdentity);
                        cboundIdentity.StartTrackingLifetime((ILease)Obj.InitializeLifetimeService());
                        return(cboundIdentity.CreateObjRef(RequestedType));
                    }
                    else if (ObjURI != null)
                    {
                        throw new RemotingException("It is not possible marshal a proxy of a remote object.");
                    }

                    ObjRef or = proxy.ObjectIdentity.CreateObjRef(RequestedType);
                    TrackingServices.NotifyMarshaledObject(Obj, or);
                    return(or);
                }
            }

            if (RequestedType == null)
            {
                RequestedType = Obj.GetType();
            }

            if (ObjURI == null)
            {
                if (Obj.ObjectIdentity == null)
                {
                    ObjURI = NewUri();
                    CreateClientActivatedServerIdentity(Obj, RequestedType, ObjURI);
                }
            }
            else
            {
                ClientActivatedIdentity identity = GetIdentityForUri("/" + ObjURI) as ClientActivatedIdentity;
                if (identity == null || Obj != identity.GetServerObject())
                {
                    CreateClientActivatedServerIdentity(Obj, RequestedType, ObjURI);
                }
            }

            ObjRef oref;

            if (IsTransparentProxy(Obj))
            {
                oref = RemotingServices.GetRealProxy(Obj).ObjectIdentity.CreateObjRef(RequestedType);
            }
            else
            {
                oref = Obj.CreateObjRef(RequestedType);
            }

            TrackingServices.NotifyMarshaledObject(Obj, oref);
            return(oref);
        }
Ejemplo n.º 19
0
 internal static bool ProxyCheckCast(RealProxy rp, RuntimeType castType)
 {
     // TODO: What should it do?
     return(true);
 }
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            if (!this.IsSingleCall())
            {
                if (this._serverObjectChain == null)
                {
                    bool flag = false;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        Monitor.Enter(this, ref flag);
                        if (this._serverObjectChain == null)
                        {
                            MarshalByRefObject tporObject = base.TPOrObject;
                            this._serverObjectChain = this._srvCtx.CreateServerObjectChain(tporObject);
                        }
                    }
                    finally
                    {
                        if (flag)
                        {
                            Monitor.Exit(this);
                        }
                    }
                }
                return(this._serverObjectChain);
            }
            MarshalByRefObject marshalByRefObject;
            IMessageSink       messageSink;

            if (this._tpOrObject != null && this._firstCallDispatched == 0 && Interlocked.CompareExchange(ref this._firstCallDispatched, 1, 0) == 0)
            {
                marshalByRefObject = (MarshalByRefObject)this._tpOrObject;
                messageSink        = this._serverObjectChain;
                if (messageSink == null)
                {
                    messageSink = this._srvCtx.CreateServerObjectChain(marshalByRefObject);
                }
            }
            else
            {
                marshalByRefObject = (MarshalByRefObject)Activator.CreateInstance(this._srvType, true);
                string objectUri = RemotingServices.GetObjectUri(marshalByRefObject);
                if (objectUri != null)
                {
                    throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CtorCantMarshal"), base.URI));
                }
                if (!RemotingServices.IsTransparentProxy(marshalByRefObject))
                {
                    marshalByRefObject.__RaceSetServerIdentity(this);
                }
                else
                {
                    RealProxy realProxy = RemotingServices.GetRealProxy(marshalByRefObject);
                    realProxy.IdentityObject = this;
                }
                messageSink = this._srvCtx.CreateServerObjectChain(marshalByRefObject);
            }
            obj = marshalByRefObject;
            return(messageSink);
        }
Ejemplo n.º 21
0
 // Determine if an object is a transparent proxy.
 public static bool IsTransparentProxy(Object proxy)
 {
     return(RealProxy.IsTransparentProxy(proxy));
 }
Ejemplo n.º 22
0
        } // CasualResolveReference

        //
        //
        // This is typically called when we need to create/establish
        // an identity for a serverObject.
        internal static ServerIdentity FindOrCreateServerIdentity(
            MarshalByRefObject obj, String objURI, int flags)
        {
            Message.DebugOut("Entered FindOrCreateServerIdentity \n");

            ServerIdentity srvID = null;
            bool           bLock = false;

            try
            {
                bool fServer;
                srvID = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

                if (srvID == null)
                {
                    // Create a new server identity and add it to the
                    // table. IdentityHolder will take care of races
                    Context serverCtx = null;

                    if (obj is ContextBoundObject)
                    {
                        serverCtx = Thread.CurrentContext;
                    }
                    else
                    {
                        serverCtx = DefaultContext;
                    }
                    BCLDebug.Assert(null != serverCtx, "null != serverCtx");

                    ServerIdentity serverID = new ServerIdentity(obj, serverCtx);

                    // Set the identity depending on whether we have the server or proxy
                    if (fServer)
                    {
                        srvID = obj.__RaceSetServerIdentity(serverID);
                        BCLDebug.Assert(srvID == MarshalByRefObject.GetIdentity(obj), "Bad ID state!");
                    }
                    else
                    {
                        RealProxy rp = null;
                        rp = RemotingServices.GetRealProxy(obj);
                        BCLDebug.Assert(null != rp, "null != rp");

                        rp.IdentityObject = serverID;
                        srvID             = (ServerIdentity)rp.IdentityObject;
                    }

                    Message.DebugOut("Created ServerIdentity \n");
                }


                // NOTE: for purely x-context cases we never execute this ...
                // the server ID is not put in the ID table.
                if (IdOps.bStrongIdentity(flags))
                {
                    TableLock.AcquireWriterLock(INFINITE);
                    bLock = true;

                    // It is possible that we are marshaling out of this app-domain
                    // for the first time. We need to do two things
                    // (1) If there is no URI associated with the identity then go ahead
                    // and generate one.
                    // (2) Add the identity to the URI -> Identity map if not already present
                    // (For purely x-context cases we don't need the URI)
                    // (3) If the object ref is null, then this object hasn't been
                    // marshalled yet.
                    // (4) if id was created through SetObjectUriForMarshal, it would be
                    // in the ID table
                    if ((srvID.ObjURI == null) ||
                        (srvID.IsInIDTable() == false))
                    {
                        // we are marshalling a server object, so there should not be a
                        //   a different identity at this location.
                        SetIdentity(srvID, objURI, DuplicateIdentityOption.Unique);
                    }

                    // If the object is marked as disconnect, mark it as connected
                    if (srvID.IsDisconnected())
                    {
                        srvID.SetFullyConnected();
                    }
                }
            }
            finally
            {
                if (bLock)
                {
                    TableLock.ReleaseWriterLock();
                }
            }

            Message.DebugOut("Leaving FindOrCreateServerIdentity \n");
            BCLDebug.Assert(null != srvID, "null != srvID");
            return(srvID);
        }
Ejemplo n.º 23
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            // NOTE: Lifetime relies on the Identity flags for
            // SingleCall and Singleton being set by the time this getter
            // is called.
            // get
            // {
            if (!this.IsSingleCall())
            {
                // This is the common case
                if (_serverObjectChain == null)
                {
                    lock (this)
                    {
                        if (_serverObjectChain == null)
                        {
                            MarshalByRefObject srvObj =
                                (MarshalByRefObject)
                                this.TPOrObject;

                            _serverObjectChain =
                                _srvCtx.CreateServerObjectChain(
                                    srvObj);
                        }
                    }
                }
                BCLDebug.Assert(null != _serverObjectChain,
                                "null != _serverObjectChain");

                return(_serverObjectChain);
            }
            else
            {
                // ---------- SINGLE CALL WKO --------------
                // In this case, we are expected to provide
                // a fresh server object for each dispatch.
                // Since the server object chain is object
                // specific, we must create a fresh chain too.

                // We must be in the correct context for this
                // to succeed.


                BCLDebug.Assert(Thread.CurrentContext == _srvCtx,
                                "Bad context mismatch");

                // For singleCall we create a fresh object & its chain
                // on each dispatch!
                MarshalByRefObject srvObj = (MarshalByRefObject)
                                            Activator.CreateInstance((Type)_srvType, true);

                // make sure that object didn't Marshal itself.
                // (well known objects should live up to their promise
                // of exporting themselves through exactly one url)
                String tempUri = RemotingServices.GetObjectUri(srvObj);
                if (tempUri != null)
                {
                    throw new RemotingException(
                              String.Format(
                                  Environment.GetResourceString(
                                      "Remoting_WellKnown_CtorCantMarshal"),
                                  this.URI));
                }

                // Set the identity depending on whether we have the server
                // or proxy
                if (!RemotingServices.IsTransparentProxy(srvObj))
                {
#if _DEBUG
                    Identity idObj = srvObj.__RaceSetServerIdentity(this);
#else
                    srvObj.__RaceSetServerIdentity(this);
#endif
#if _DEBUG
                    BCLDebug.Assert(idObj == this, "Bad ID state!");
                    BCLDebug.Assert(idObj == MarshalByRefObject.GetIdentity(srvObj), "Bad ID state!");
#endif
                }
                else
                {
                    RealProxy rp = null;
                    rp = RemotingServices.GetRealProxy(srvObj);
                    BCLDebug.Assert(null != rp, "null != rp");
//  #if _DEBUG
//                      Identity idObj = (ServerIdentity) rp.SetIdentity(this);
// #else
                    rp.IdentityObject = this;
// #endif
                }

                // This is passed out to the caller so that for single-call
                // case we can call Dispose when the incoming call is done
                obj = srvObj;
                // Create the object chain and return it
                return(_srvCtx.CreateServerObjectChain(srvObj));
            }
            // }
        }
Ejemplo n.º 24
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static ServerIdentity FindOrCreateServerIdentity(
            MarshalByRefObject obj, String objURI, int flags)
        {
            Message.DebugOut("Entered FindOrCreateServerIdentity \n");

            ServerIdentity srvID = null;

            bool fServer;

            srvID = (ServerIdentity)MarshalByRefObject.GetIdentity(obj, out fServer);

            if (srvID == null)
            {
                // Create a new server identity and add it to the
                // table. IdentityHolder will take care of ----s
                Context serverCtx = null;

                if (obj is ContextBoundObject)
                {
                    serverCtx = Thread.CurrentContext;
                }
                else
                {
                    serverCtx = DefaultContext;
                }
                Contract.Assert(null != serverCtx, "null != serverCtx");

                ServerIdentity serverID = new ServerIdentity(obj, serverCtx);

                // Set the identity depending on whether we have the server or proxy
                if (fServer)
                {
                    srvID = obj.__RaceSetServerIdentity(serverID);
                    Contract.Assert(srvID == MarshalByRefObject.GetIdentity(obj), "Bad ID state!");
                }
                else
                {
                    RealProxy rp = null;
                    rp = RemotingServices.GetRealProxy(obj);
                    Contract.Assert(null != rp, "null != rp");

                    rp.IdentityObject = serverID;
                    srvID             = (ServerIdentity)rp.IdentityObject;
                }

                // DevDiv 720951 and 911924:
                // CreateWellKnownObject creates a ServerIdentity and places it in URITable
                // before it is fully initialized.  This transient flag is set to to prevent
                // other concurrent operations from using it.  CreateWellKnownObject is the
                // only code path that sets this flag, and by default it is false.
                if (IdOps.bIsInitializing(flags))
                {
                    srvID.IsInitializing = true;
                }

                Message.DebugOut("Created ServerIdentity \n");
            }

#if false
            // Check that we are asked to create the identity for the same
            // URI as the one already associated with the server object.
            // It is an error to associate two URIs with the same server
            // object
            // GopalK: Try eliminating the test because it is also done by GetOrCreateIdentity
            if ((null != objURI) && (null != srvID.ObjURI))
            {
                if (string.Compare(objURI, srvID.ObjURI, StringComparison.OrdinalIgnoreCase) == 0) // case-insensitive compare
                {
                    Message.DebugOut("Trying to associate a URI with identity again .. throwing execption \n");
                    throw new RemotingException(
                              String.Format(
                                  Environment.GetResourceString(
                                      "Remoting_ResetURI"),
                                  srvID.ObjURI, objURI));
                }
            }
#endif

            // NOTE: for purely x-context cases we never execute this ...
            // the server ID is not put in the ID table.
            if (IdOps.bStrongIdentity(flags))
            {
                // We need to guarantee that finally is not interrupted so that the lock is released.
                // TableLock has a long path without reliability contract.  To avoid adding contract on
                // the path, we will use ReaderWriterLock directly.
                ReaderWriterLock rwlock         = TableLock;
                bool             takeAndRelease = !rwlock.IsWriterLockHeld;

                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                    if (takeAndRelease)
                    {
                        rwlock.AcquireWriterLock(INFINITE);
                    }

                    // It is possible that we are marshaling out of this app-domain
                    // for the first time. We need to do two things
                    // (1) If there is no URI associated with the identity then go ahead
                    // and generate one.
                    // (2) Add the identity to the URI -> Identity map if not already present
                    // (For purely x-context cases we don't need the URI)
                    // (3) If the object ref is null, then this object hasn't been
                    // marshalled yet.
                    // (4) if id was created through SetObjectUriForMarshal, it would be
                    // in the ID table
                    if ((srvID.ObjURI == null) ||
                        (srvID.IsInIDTable() == false))
                    {
                        // we are marshalling a server object, so there should not be a
                        //   a different identity at this location.
                        SetIdentity(srvID, objURI, DuplicateIdentityOption.Unique);
                    }

                    // If the object is marked as disconnect, mark it as connected
                    if (srvID.IsDisconnected())
                    {
                        srvID.SetFullyConnected();
                    }
                }
                finally
                {
                    if (takeAndRelease && rwlock.IsWriterLockHeld)
                    {
                        rwlock.ReleaseWriterLock();
                    }
                }
            }

            Message.DebugOut("Leaving FindOrCreateServerIdentity \n");
            Contract.Assert(null != srvID, "null != srvID");
            return(srvID);
        }
Ejemplo n.º 25
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            // NOTE: Lifetime relies on the Identity flags for
            // SingleCall and Singleton being set by the time this getter
            // is called.
            if (!this.IsSingleCall())
            {
                // This is the common case
                if (_serverObjectChain == null)
                {
                    lock (this)
                    {
                        if (_serverObjectChain == null)
                        {
                            MarshalByRefObject srvObj =
                                (MarshalByRefObject)
                                this.TPOrObject;

                            _serverObjectChain =
                                _srvCtx.CreateServerObjectChain(
                                    srvObj);
                        }
                    }
                }
                BCLDebug.Assert(null != _serverObjectChain,
                                "null != _serverObjectChain");

                return(_serverObjectChain);
            }
            else
            {
                // ---------- SINGLE CALL WKO --------------
                // In this case, we are expected to provide
                // a fresh server object for each dispatch.
                // Since the server object chain is object
                // specific, we must create a fresh chain too.

                // We must be in the correct context for this
                // to succeed.

                // NOTE: It may be useful to recycle the context too
                // but for V1 we will keep the context we setup for
                // the first object around and always dispatch the call
                // through that context ... when we are here we have
                // already been through the server sink chain. We will
                // only dispatch through the object sink chain of the
                // object we will create below ... if for some reason
                // the object creates its own context we will not go
                // through the server sink chain for that context again.
                // We should fix this in V-next and always create everything
                // from scratch for single-call wellKnown objects

                BCLDebug.Assert(Thread.CurrentContext == _srvCtx,
                                "Bad context mismatch");
                MarshalByRefObject srvObj   = null;
                IMessageSink       objChain = null;
                if (_tpOrObject != null && _firstCallDispatched == 0 && Interlocked.CompareExchange(ref _firstCallDispatched, 1, 0) == 0)
                {
                    // use the instance of server object created to
                    // set up the pipeline.
                    srvObj = (MarshalByRefObject)_tpOrObject;

                    objChain = _serverObjectChain;

                    if (objChain == null)
                    {
                        objChain = _srvCtx.CreateServerObjectChain(srvObj);
                    }
                }
                else
                {
                    // For singleCall we create a fresh object & its chain
                    // on each dispatch!
                    srvObj = (MarshalByRefObject)
                             Activator.CreateInstance((Type)_srvType, true);

                    // make sure that object didn't Marshal itself.
                    // (well known objects should live up to their promise
                    // of exporting themselves through exactly one url)
                    String tempUri = RemotingServices.GetObjectUri(srvObj);
                    if (tempUri != null)
                    {
                        throw new RemotingException(
                                  String.Format(
                                      Environment.GetResourceString(
                                          "Remoting_WellKnown_CtorCantMarshal"),
                                      this.URI));
                    }

                    // Set the identity depending on whether we have the server
                    // or proxy
                    if (!RemotingServices.IsTransparentProxy(srvObj))
                    {
#if _DEBUG
                        Identity idObj = srvObj.__RaceSetServerIdentity(this);
#else
                        srvObj.__RaceSetServerIdentity(this);
#endif
#if _DEBUG
                        BCLDebug.Assert(idObj == this, "Bad ID state!");
                        BCLDebug.Assert(idObj == MarshalByRefObject.GetIdentity(srvObj), "Bad ID state!");
#endif
                    }
                    else
                    {
                        RealProxy rp = null;
                        rp = RemotingServices.GetRealProxy(srvObj);
                        BCLDebug.Assert(null != rp, "null != rp");
                        //  #if _DEBUG
                        //                      Identity idObj = (ServerIdentity) rp.SetIdentity(this);
                        // #else
                        rp.IdentityObject = this;
                        // #endif
#if _DEBUG
                        // REVIEW: turn these on after adding a clean way to
                        // reset identity-s on x-context proxies ... (id needs
                        // to be set in the proxy && on the _serverObject in
                        // the proxy)
                        // BCLDebug.Assert(idObj == this, "Bad ID state!" );
                        // BCLDebug.Assert(idObj == rp.IdentityObject, "Bad ID state!" );
#endif
                    }
                    // Create the object chain and return it
                    objChain = _srvCtx.CreateServerObjectChain(srvObj);
                }

                // This is passed out to the caller so that for single-call
                // case we can call Dispose when the incoming call is done
                obj = srvObj;
                return(objChain);
            }
        }