[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;
}
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;
}