System.Runtime.Remoting.IdentityHolder.FindOrCreateServerIdentity C# (CSharp) Method

FindOrCreateServerIdentity() static private method

static private FindOrCreateServerIdentity ( MarshalByRefObject obj, String objURI, int flags ) : ServerIdentity
obj System.MarshalByRefObject
objURI String
flags int
return ServerIdentity
        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 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) )
            {
                // 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");
            BCLDebug.Assert(null != srvID,"null != srvID");
            return srvID;                
        }