private static Identity GetOrCreateIdentity(
MarshalByRefObject Obj,
String ObjURI)
{
Identity idObj = null;
if (IsTransparentProxy(Obj))
{
Message.DebugOut("Marshaling proxy \n");
RealProxy realProxy = GetRealProxy(Obj);
BCLDebug.Assert(null != realProxy,"null != realProxy");
idObj = realProxy.IdentityObject;
if(null == idObj)
{
// passing the strongIdentity flag will ensure that a weak
// reference to the identity will get converted to a strong
// one so as to keep the object alive (and in control of
// lifeTimeServices)
idObj = IdentityHolder.FindOrCreateServerIdentity(
Obj,
ObjURI,
IdOps.StrongIdentity);
idObj.RaceSetTransparentProxy(Obj);
}
// At this point of time we should have an identity
BCLDebug.Assert(null != idObj, "null != idObj");
ServerIdentity srvID = idObj as ServerIdentity;
if (srvID != null)
{
// We are marshaling a proxy with a serverIdentity
// associated with it but with no URI generated yet.
// For a genuine proxy case we will always have a URI
// AND it will be a client ID!
// So if we are here this must be a SrvID
// for a ContextBoundObject
BCLDebug.Assert(srvID != null && Obj is ContextBoundObject,
"This case should only be hit for ContextBoundObjects & ServerIdentity!");
Message.DebugOut("Marshal::Looking up server side identity \n");
#if _DEBUG
srvID.AssertValid();
#endif
// passing the strongIdentity flag will ensure that a weak
// reference to the identity will get converted to a strong
// one so as to keep the object alive (and in control of
// lifeTimeServices)
idObj = IdentityHolder.FindOrCreateServerIdentity(
srvID.TPOrObject,
ObjURI,
IdOps.StrongIdentity);
// if an objURI was specified we need to make sure that
// the same one was used.
if ((null != ObjURI) &&
(ObjURI != Identity.RemoveAppNameOrAppGuidIfNecessary(
idObj.ObjURI)))
{
throw new RemotingException(
Environment.GetResourceString(
"Remoting_URIExists"));
}
// We create a unique ID for an object and never
// change it!
BCLDebug.Assert(srvID == idObj, "Bad ID Table state!");
}
else
{
// We are marshaling a proxy with a (client)Identity associated with it
// It must already have a URI.
Message.DebugOut("Marshal::Client side identity \n");
BCLDebug.Assert(idObj.ObjURI != null, "Client side id without URI!");
//
// One cannot associate a URI with a proxy generated by us
// because either a URI was generated at the server side for the object
// it represents or it is a well known object in which case the URI is known.
//
// For custom proxies this restriction is relaxed because we treat the
// transparent proxy as the server object for such cases.
//
if ((null != ObjURI) && (ObjURI != idObj.ObjURI))
{
throw new RemotingException(
Environment.GetResourceString(
"Remoting_URIToProxy"));
}
}
BCLDebug.Assert(null != idObj.ObjURI,"null != idObj.ObjURI");
}
else
{
// Find or Add the object identity to the identity table
Message.DebugOut("Marshaling object \n");
// passing the strongIdentity flag will ensure that a weak
// reference to the identity will get converted to a strong
// one so as to keep the object alive (and in control of
// lifeTimeServices)
// The object may have an ID if it was marshaled but its lease
// timed out.
#if _DEBUG
ServerIdentity idTmp =
(ServerIdentity) MarshalByRefObject.GetIdentity(Obj);
#endif
idObj = IdentityHolder.FindOrCreateServerIdentity(
Obj,
ObjURI,
IdOps.StrongIdentity);
// If the object had an ID to begin with that is the one
// we must have set in the table.
#if _DEBUG
BCLDebug.Assert(idTmp==null || idTmp == idObj, "Bad ID Table state!");
#endif
}
return idObj;
}