GetRealProxy() private méthode

private GetRealProxy ( Object proxy ) : RealProxy
proxy Object
Résultat System.Runtime.Remoting.Proxies.RealProxy
Exemple #1
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 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"));
                }
            }
        }
        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);
        }
Exemple #4
0
        internal IMessageSink GetServerObjectChain(out MarshalByRefObject obj)
        {
            obj = null;
            if (!this.IsSingleCall())
            {
                if (this._serverObjectChain == null)
                {
                    bool lockTaken = false;
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try
                    {
                        Monitor.Enter(this, ref lockTaken);
                        if (this._serverObjectChain == null)
                        {
                            MarshalByRefObject tPOrObject = base.TPOrObject;
                            this._serverObjectChain = this._srvCtx.CreateServerObjectChain(tPOrObject);
                        }
                    }
                    finally
                    {
                        if (lockTaken)
                        {
                            Monitor.Exit(this);
                        }
                    }
                }
                return(this._serverObjectChain);
            }
            MarshalByRefObject serverObj = null;
            IMessageSink       sink      = null;

            if (((base._tpOrObject != null) && (this._firstCallDispatched == 0)) && (Interlocked.CompareExchange(ref this._firstCallDispatched, 1, 0) == 0))
            {
                serverObj = (MarshalByRefObject)base._tpOrObject;
                sink      = this._serverObjectChain;
                if (sink == null)
                {
                    sink = this._srvCtx.CreateServerObjectChain(serverObj);
                }
            }
            else
            {
                serverObj = (MarshalByRefObject)Activator.CreateInstance(this._srvType, true);
                if (RemotingServices.GetObjectUri(serverObj) != null)
                {
                    throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_WellKnown_CtorCantMarshal"), new object[] { base.URI }));
                }
                if (!RemotingServices.IsTransparentProxy(serverObj))
                {
                    serverObj.__RaceSetServerIdentity(this);
                }
                else
                {
                    RemotingServices.GetRealProxy(serverObj).IdentityObject = this;
                }
                sink = this._srvCtx.CreateServerObjectChain(serverObj);
            }
            obj = serverObj;
            return(sink);
        }
Exemple #5
0
        public void AttachServerObject(MarshalByRefObject serverObject, Context context)
        {
            DisposeServerObject();

            _context      = context;
            _serverObject = serverObject;

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

                _serverObject.ObjectIdentity = this;
            }
        }
Exemple #6
0
 internal RedirectionProxy(MarshalByRefObject proxy, Type serverType)
 {
     this._proxy      = proxy;
     this._realProxy  = RemotingServices.GetRealProxy(this._proxy);
     this._serverType = serverType;
     this._objectMode = WellKnownObjectMode.Singleton;
 }
Exemple #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);
        }
 internal ServerIdentity(MarshalByRefObject obj, Context serverCtx)
     : base(obj is ContextBoundObject)
 {
     if (obj != null)
     {
         this._srvType = RemotingServices.IsTransparentProxy((object)obj) ? RemotingServices.GetRealProxy((object)obj).GetProxiedType() : obj.GetType();
     }
     this._srvCtx            = serverCtx;
     this._serverObjectChain = (IMessageSink)null;
     this._stackBuilderSink  = (StackBuilderSink)null;
 }
Exemple #9
0
        internal static bool RemoveDynamicProperty(MarshalByRefObject obj, string name)
        {
            if (RemotingServices.IsObjectOutOfContext((object)obj))
            {
                return(RemotingServices.GetRealProxy((object)obj).IdentityObject.RemoveProxySideDynamicProperty(name));
            }
            ServerIdentity serverIdentity = (ServerIdentity)MarshalByRefObject.GetIdentity((MarshalByRefObject)RemotingServices.AlwaysUnwrap((ContextBoundObject)obj));

            if (serverIdentity != null)
            {
                return(serverIdentity.RemoveServerSideDynamicProperty(name));
            }
            throw new RemotingException(Environment.GetResourceString("Remoting_NoIdentityEntry"));
        }
        internal static bool AddDynamicProperty(MarshalByRefObject obj, IDynamicProperty prop)
        {
            if (RemotingServices.IsObjectOutOfContext(obj))
            {
                return(RemotingServices.GetRealProxy(obj).IdentityObject.AddProxySideDynamicProperty(prop));
            }
            MarshalByRefObject obj2     = (MarshalByRefObject)RemotingServices.AlwaysUnwrap((ContextBoundObject)obj);
            ServerIdentity     identity = (ServerIdentity)MarshalByRefObject.GetIdentity(obj2);

            if (identity == null)
            {
                throw new RemotingException(Environment.GetResourceString("Remoting_NoIdentityEntry"));
            }
            return(identity.AddServerSideDynamicProperty(prop));
        }
Exemple #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);
        }
        public virtual IMessage SyncProcessMessage(IMessage msg)
        {
            IMessage message;

            try
            {
                msg.Properties[(object)"__Uri"] = (object)this._realProxy.IdentityObject.URI;
                message = this._objectMode != WellKnownObjectMode.Singleton ? RemotingServices.GetRealProxy((object)(MarshalByRefObject)Activator.CreateInstance(this._serverType, true)).Invoke(msg) : this._realProxy.Invoke(msg);
            }
            catch (Exception ex)
            {
                IMethodCallMessage mcm = msg as IMethodCallMessage;
                message = (IMessage) new ReturnMessage(ex, mcm);
            }
            return(message);
        }
Exemple #13
0
 public virtual IMessage SyncProcessMessage(IMessage msg)
 {
     try
     {
         msg.Properties["__Uri"] = this._realProxy.IdentityObject.URI;
         if (this._objectMode == WellKnownObjectMode.Singleton)
         {
             return(this._realProxy.Invoke(msg));
         }
         MarshalByRefObject proxy = (MarshalByRefObject)Activator.CreateInstance(this._serverType, true);
         return(RemotingServices.GetRealProxy(proxy).Invoke(msg));
     }
     catch (Exception exception)
     {
         return(new ReturnMessage(exception, msg as IMethodCallMessage));
     }
 }
 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;
 }
Exemple #15
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;
        }
 public void AttachServerObject(MarshalByRefObject serverObject, Context context)
 {
     this.DisposeServerObject();
     this._context      = context;
     this._serverObject = serverObject;
     if (RemotingServices.IsTransparentProxy(serverObject))
     {
         RealProxy realProxy = RemotingServices.GetRealProxy(serverObject);
         if (realProxy.ObjectIdentity == null)
         {
             realProxy.ObjectIdentity = this;
         }
     }
     else
     {
         if (this._objectType.IsContextful)
         {
             this._envoySink = context.CreateEnvoySink(serverObject);
         }
         this._serverObject.ObjectIdentity = this;
     }
 }
Exemple #17
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));
            }
            // }
        }
Exemple #18
0
        internal void Init(object o, Identity idObj, RuntimeType requestedType)
        {
            this.uri = idObj.URI;
            MarshalByRefObject tPOrObject = idObj.TPOrObject;
            RuntimeType        c          = null;

            if (!RemotingServices.IsTransparentProxy(tPOrObject))
            {
                c = (RuntimeType)tPOrObject.GetType();
            }
            else
            {
                c = (RuntimeType)RemotingServices.GetRealProxy(tPOrObject).GetProxiedType();
            }
            RuntimeType typeOfObj = (null == requestedType) ? c : requestedType;

            if (((null != requestedType) && !requestedType.IsAssignableFrom(c)) && !typeof(IMessageSink).IsAssignableFrom(c))
            {
                throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), new object[] { requestedType.ToString() }));
            }
            if (c.IsCOMObject)
            {
                DynamicTypeInfo info = new DynamicTypeInfo(typeOfObj);
                this.TypeInfo = info;
            }
            else
            {
                RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(typeOfObj);
                this.TypeInfo = reflectionCachedData.TypeInfo;
            }
            if (!idObj.IsWellKnown())
            {
                this.EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
                IChannelInfo info2 = new System.Runtime.Remoting.ChannelInfo();
                if (o is AppDomain)
                {
                    object[] channelData      = info2.ChannelData;
                    int      length           = channelData.Length;
                    object[] destinationArray = new object[length];
                    Array.Copy(channelData, destinationArray, length);
                    for (int i = 0; i < length; i++)
                    {
                        if (!(destinationArray[i] is CrossAppDomainData))
                        {
                            destinationArray[i] = null;
                        }
                    }
                    info2.ChannelData = destinationArray;
                }
                this.ChannelInfo = info2;
                if (c.HasProxyAttribute)
                {
                    this.SetHasProxyAttribute();
                }
            }
            else
            {
                this.SetWellKnown();
            }
            if (ShouldUseUrlObjRef())
            {
                if (this.IsWellKnown())
                {
                    this.SetObjRefLite();
                }
                else
                {
                    string str = ChannelServices.FindFirstHttpUrlForObject(this.URI);
                    if (str != null)
                    {
                        this.URI = str;
                        this.SetObjRefLite();
                    }
                }
            }
        }
Exemple #19
0
        //
        //
        internal void Init(Identity idObj, Type requestedType)
        {
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null, "idObj != null");

            // Set the URI of the object to be marshaled
            URI = idObj.URI;

            // Figure out the type
            MarshalByRefObject obj = idObj.TPOrObject;

            BCLDebug.Assert(null != obj, "Identity not setup correctly");

            // Get the type of the object
            Type serverType = null;

            if (!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = obj.GetType();
            }
            else
            {
                serverType = RemotingServices.GetRealProxy(obj).GetProxiedType();
            }

            Type typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible
            //  (except for objects that implement IMessageSink, since we
            //   just hand off the message instead of invoking the proxy)
            if ((null != requestedType) &&
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType)))
            {
                throw new RemotingException(
                          String.Format(
                              Environment.GetResourceString(
                                  "Remoting_InvalidRequestedType"),
                              requestedType.ToString()));;
            }

            // Create the type info
            if (serverType.IsCOMObject)
            {
                // __ComObjects need dynamic TypeInfo
                DynamicTypeInfo dt = new DynamicTypeInfo(typeOfObj);
                TypeInfo = (IRemotingTypeInfo)dt;
            }
            else
            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                                               InternalRemotingServices.GetReflectionCachedData(typeOfObj);

                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            }

            if (!idObj.IsWellKnown())
            {
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info
                ChannelInfo = (IChannelInfo) new ChannelInfo();

                if (serverType.HasProxyAttribute)
                {
                    SetHasProxyAttribute();
                }
            }
            else
            {
                SetWellKnown();
            }

            // See if we should and can use a url obj ref?
            if (ShouldUseUrlObjRef())
            {
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite();
                }
                else
                {
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null)
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    }
                }
            }
        } // Init
Exemple #20
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);
        }
Exemple #21
0
        //
        //
        internal void Init(Object o, Identity idObj, Type requestedType)
        {
            Message.DebugOut("RemotingServices::FillObjRef: IN");
            BCLDebug.Assert(idObj != null, "idObj != null");

            // Set the URI of the object to be marshaled
            uri = idObj.URI;

            // Figure out the type
            MarshalByRefObject obj = idObj.TPOrObject;

            BCLDebug.Assert(null != obj, "Identity not setup correctly");

            // Get the type of the object
            Type serverType = null;

            if (!RemotingServices.IsTransparentProxy(obj))
            {
                serverType = obj.GetType();
            }
            else
            {
                serverType = RemotingServices.GetRealProxy(obj).GetProxiedType();
            }

            Type typeOfObj = (null == requestedType ? serverType : requestedType);

            // Make sure that the server and requested types are compatible
            //  (except for objects that implement IMessageSink, since we
            //   just hand off the message instead of invoking the proxy)
            if ((null != requestedType) &&
                !requestedType.IsAssignableFrom(serverType) &&
                (!typeof(IMessageSink).IsAssignableFrom(serverType)))
            {
                throw new RemotingException(
                          String.Format(
                              CultureInfo.CurrentCulture, Environment.GetResourceString(
                                  "Remoting_InvalidRequestedType"),
                              requestedType.ToString()));;
            }

            {
                RemotingTypeCachedData cache = (RemotingTypeCachedData)
                                               InternalRemotingServices.GetReflectionCachedData(typeOfObj);

                TypeInfo = (IRemotingTypeInfo)cache.TypeInfo;
            }

            if (!idObj.IsWellKnown())
            {
                // Create the envoy info
                EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);

                // Create the channel info
                IChannelInfo chan = (IChannelInfo) new ChannelInfo();
                // Make sure the channelInfo only has x-appdomain data since the objref is agile while other
                // channelData might not be and regardless this data is useless for an appdomain proxy
                if (o is AppDomain)
                {
                    Object[] channelData       = chan.ChannelData;
                    int      channelDataLength = channelData.Length;
                    Object[] newChannelData    = new Object[channelDataLength];
                    Array.Copy(channelData, newChannelData, channelDataLength);
                    for (int i = 0; i < channelDataLength; i++)
                    {
                        if (!(newChannelData[i] is CrossAppDomainData))
                        {
                            newChannelData[i] = null;
                        }
                    }
                    chan.ChannelData = newChannelData;
                }
                ChannelInfo = chan;

                if (serverType.HasProxyAttribute)
                {
                    SetHasProxyAttribute();
                }
            }
            else
            {
                SetWellKnown();
            }

            // See if we should and can use a url obj ref?
            if (ShouldUseUrlObjRef())
            {
                if (IsWellKnown())
                {
                    // full uri already supplied.
                    SetObjRefLite();
                }
                else
                {
                    String httpUri = ChannelServices.FindFirstHttpUrlForObject(URI);
                    if (httpUri != null)
                    {
                        URI = httpUri;
                        SetObjRefLite();
                    }
                }
            }
        } // Init
Exemple #22
0
        internal void Init(object o, Identity idObj, RuntimeType requestedType)
        {
            this.uri = idObj.URI;
            MarshalByRefObject tpOrObject   = idObj.TPOrObject;
            RuntimeType        runtimeType1 = RemotingServices.IsTransparentProxy((object)tpOrObject) ? (RuntimeType)RemotingServices.GetRealProxy((object)tpOrObject).GetProxiedType() : (RuntimeType)tpOrObject.GetType();
            RuntimeType        runtimeType2 = (RuntimeType)null == requestedType ? runtimeType1 : requestedType;

            if ((RuntimeType)null != requestedType && !requestedType.IsAssignableFrom((System.Reflection.TypeInfo)runtimeType1) && !typeof(IMessageSink).IsAssignableFrom((Type)runtimeType1))
            {
                throw new RemotingException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), (object)requestedType.ToString()));
            }
            this.TypeInfo = !runtimeType1.IsCOMObject ? (IRemotingTypeInfo)InternalRemotingServices.GetReflectionCachedData(runtimeType2).TypeInfo : (IRemotingTypeInfo) new DynamicTypeInfo(runtimeType2);
            if (!idObj.IsWellKnown())
            {
                this.EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
                IChannelInfo channelInfo = (IChannelInfo) new System.Runtime.Remoting.ChannelInfo();
                if (o is AppDomain)
                {
                    object[] channelData = channelInfo.ChannelData;
                    int      length1     = channelData.Length;
                    object[] objArray1   = new object[length1];
                    object[] objArray2   = objArray1;
                    int      length2     = length1;
                    Array.Copy((Array)channelData, (Array)objArray2, length2);
                    for (int index = 0; index < length1; ++index)
                    {
                        if (!(objArray1[index] is CrossAppDomainData))
                        {
                            objArray1[index] = (object)null;
                        }
                    }
                    channelInfo.ChannelData = objArray1;
                }
                this.ChannelInfo = channelInfo;
                if (runtimeType1.HasProxyAttribute)
                {
                    this.SetHasProxyAttribute();
                }
            }
            else
            {
                this.SetWellKnown();
            }
            if (!ObjRef.ShouldUseUrlObjRef())
            {
                return;
            }
            if (this.IsWellKnown())
            {
                this.SetObjRefLite();
            }
            else
            {
                string httpUrlForObject = ChannelServices.FindFirstHttpUrlForObject(this.URI);
                if (httpUrlForObject == null)
                {
                    return;
                }
                this.URI = httpUrlForObject;
                this.SetObjRefLite();
            }
        }
        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);
        }
        internal void Init(object o, Identity idObj, RuntimeType requestedType)
        {
            this.uri = idObj.URI;
            MarshalByRefObject tporObject = idObj.TPOrObject;
            RuntimeType        runtimeType;

            if (!RemotingServices.IsTransparentProxy(tporObject))
            {
                runtimeType = (RuntimeType)tporObject.GetType();
            }
            else
            {
                runtimeType = (RuntimeType)RemotingServices.GetRealProxy(tporObject).GetProxiedType();
            }
            RuntimeType runtimeType2 = (null == requestedType) ? runtimeType : requestedType;

            if (null != requestedType && !requestedType.IsAssignableFrom(runtimeType) && !typeof(IMessageSink).IsAssignableFrom(runtimeType))
            {
                throw new RemotingException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Remoting_InvalidRequestedType"), requestedType.ToString()));
            }
            if (runtimeType.IsCOMObject)
            {
                DynamicTypeInfo dynamicTypeInfo = new DynamicTypeInfo(runtimeType2);
                this.TypeInfo = dynamicTypeInfo;
            }
            else
            {
                RemotingTypeCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(runtimeType2);
                this.TypeInfo = reflectionCachedData.TypeInfo;
            }
            if (!idObj.IsWellKnown())
            {
                this.EnvoyInfo = System.Runtime.Remoting.EnvoyInfo.CreateEnvoyInfo(idObj as ServerIdentity);
                IChannelInfo channelInfo = new ChannelInfo();
                if (o is AppDomain)
                {
                    object[] channelData = channelInfo.ChannelData;
                    int      num         = channelData.Length;
                    object[] array       = new object[num];
                    Array.Copy(channelData, array, num);
                    for (int i = 0; i < num; i++)
                    {
                        if (!(array[i] is CrossAppDomainData))
                        {
                            array[i] = null;
                        }
                    }
                    channelInfo.ChannelData = array;
                }
                this.ChannelInfo = channelInfo;
                if (runtimeType.HasProxyAttribute)
                {
                    this.SetHasProxyAttribute();
                }
            }
            else
            {
                this.SetWellKnown();
            }
            if (ObjRef.ShouldUseUrlObjRef())
            {
                if (this.IsWellKnown())
                {
                    this.SetObjRefLite();
                    return;
                }
                string text = ChannelServices.FindFirstHttpUrlForObject(this.URI);
                if (text != null)
                {
                    this.URI = text;
                    this.SetObjRefLite();
                }
            }
        }
Exemple #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);
            }
        }
        } // 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);
        }
Exemple #27
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);
        }