예제 #1
0
        public ExecutionContext CreateCopy()
        {
            if (!isNewCapture)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CannotCopyUsedContext"));
            }
            ExecutionContext ec = new ExecutionContext();

            ec.isNewCapture = true;
            ec._syncContext = _syncContext == null ? null : _syncContext.CreateCopy();
            // capture the host execution context
            ec._hostExecutionContext = _hostExecutionContext == null ? null : _hostExecutionContext.CreateCopy();
            if (_securityContext != null)
            {
                ec._securityContext = _securityContext.CreateCopy();
                ec._securityContext.ExecutionContext = ec;
            }
            if (this._logicalCallContext != null)
            {
                LogicalCallContext lc = (LogicalCallContext)this.LogicalCallContext;
                ec.LogicalCallContext = (LogicalCallContext)lc.Clone();
            }
            if (this._illogicalCallContext != null)
            {
                IllogicalCallContext ilcc = (IllogicalCallContext)this.IllogicalCallContext;
                ec.IllogicalCallContext = (IllogicalCallContext)ilcc.Clone();
            }

            return(ec);
        }
예제 #2
0
        public static List <string> GetKeys()
        {
            List <string>      keys      = new List <string>();
            ExecutionContext   context   = ExecutionContext.Capture();
            LogicalCallContext lcc       = context.GetType().InvokeMember("LogicalCallContext", BindingFlags.NonPublic | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, context, new object[0], CultureInfo.InvariantCulture) as LogicalCallContext;
            object             clone     = lcc.Clone();
            Hashtable          datastore = clone.GetType().InvokeMember("Datastore", BindingFlags.NonPublic | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, clone, new object[0], CultureInfo.InvariantCulture) as Hashtable;

            if (datastore != null)
            {
                IDictionaryEnumerator enumerator = datastore.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    keys.Add((string)enumerator.Key);
                }
            }
            return(keys);
        }
예제 #3
0
        // internal helper to capture the current execution context using a passed in stack mark
        static internal ExecutionContext Capture(ref StackCrawlMark stackMark)
        {
            // check to see if Flow is suppressed
            if (IsFlowSuppressed())
            {
                return(null);
            }

            ExecutionContext ecCurrent = Thread.CurrentThread.GetExecutionContextNoCreate();
            ExecutionContext ecNew     = new ExecutionContext();

            ecNew.isNewCapture = true;

            // capture the security context
            ecNew.SecurityContext = SecurityContext.Capture(ecCurrent, ref stackMark);
            if (ecNew.SecurityContext != null)
            {
                ecNew.SecurityContext.ExecutionContext = ecNew;
            }

            // capture the host execution context
            ecNew._hostExecutionContext = HostExecutionContextManager.CaptureHostExecutionContext();


            if (ecCurrent != null)
            {
                // capture the sync context
                ecNew._syncContext = (ecCurrent._syncContext == null) ? null : ecCurrent._syncContext.CreateCopy();

                // copy over the Logical Call Context
                if (ecCurrent._logicalCallContext != null)
                {
                    LogicalCallContext lc = (LogicalCallContext)ecCurrent.LogicalCallContext;
                    ecNew.LogicalCallContext = (LogicalCallContext)lc.Clone();
                }
            }
            return(ecNew);
        }
예제 #4
0
        /// <summary>
        /// Get all DataContract object from the LogicalCallContext
        /// </summary>
        /// <returns>Dictionary all LogicalCallContext objects</returns>
        public static Dictionary <DataContractAttribute, object> GetDataContract()
        {
            Dictionary <DataContractAttribute, object> dic = new Dictionary <DataContractAttribute, object>();
            ExecutionContext   context   = ExecutionContext.Capture();
            LogicalCallContext lcc       = context.GetType().InvokeMember("LogicalCallContext", BindingFlags.NonPublic | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, context, new object[0], CultureInfo.InvariantCulture) as LogicalCallContext;
            object             clone     = lcc.Clone();
            Hashtable          datastore = clone.GetType().InvokeMember("Datastore", BindingFlags.NonPublic | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance, null, clone, new object[0], CultureInfo.InvariantCulture) as Hashtable;

            if (datastore != null)
            {
                IDictionaryEnumerator enumerator = datastore.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    object[] attributes = enumerator.Value.GetType().GetCustomAttributes(typeof(DataContractAttribute), false);
                    if (attributes.Length > 0)
                    {
                        DataContractAttribute dca = attributes[0] as DataContractAttribute;
                        dic.Add(dca, enumerator.Value);
                    }
                }
            }
            return(dic);
        }
예제 #5
0
            /// <summary> Initializes a new instance of the <see cref="ThreadInfo"/> class. </summary>
            /// <param name="propagateThreadPrincipal"> Documentation in progress... </param>
            /// <param name="propagateCallContext"> Documentation in progress... </param>
            /// <param name="propagateCASMarkers"> Documentation in progress... </param>
            private ThreadInfo(bool propagateThreadPrincipal, bool propagateCallContext, bool propagateCASMarkers)
            {
                if (propagateThreadPrincipal)
                {
                    principal = Thread.CurrentPrincipal;
                }

                if (propagateCallContext && miGetLogicalCallContext != null)
                {
                    callContext = (LogicalCallContext)miGetLogicalCallContext.Invoke(Thread.CurrentThread, null);
                    callContext = (LogicalCallContext)callContext.Clone();

                    // TODO: consider serialize/deserialize call context to get a MBV snapshot
                    //       instead of leaving it up to the Clone method.
                }

                if (propagateCASMarkers)
                {
                    // TODO: Uncomment the following when Thread.GetCompressedStack is no longer guarded
                    //       by a StrongNameIdentityPermission.
                    //
                    // compressedStack = Thread.CurrentThread.GetCompressedStack();
                }
            }
예제 #6
0
        internal virtual IMessage InternalInvoke(IMethodCallMessage reqMcmMsg, bool useDispatchMessage, int callType)
        {
            Message message1 = reqMcmMsg as Message;

            if (message1 == null && callType != 0)
            {
                throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_InvalidCallType"));
            }
            IMessage           message2           = (IMessage)null;
            Thread             currentThread      = Thread.CurrentThread;
            LogicalCallContext logicalCallContext = currentThread.GetMutableExecutionContext().LogicalCallContext;
            Identity           identityObject     = this.IdentityObject;
            ServerIdentity     serverIdentity     = identityObject as ServerIdentity;

            if (serverIdentity != null && identityObject.IsFullyDisconnected())
            {
                throw new ArgumentException(Environment.GetResourceString("Remoting_ServerObjectNotFound", (object)reqMcmMsg.Uri));
            }
            MethodBase methodBase = reqMcmMsg.MethodBase;

            if ((MethodBase)RemotingProxy._getTypeMethod == methodBase)
            {
                return((IMessage) new ReturnMessage((object)this.GetProxiedType(), (object[])null, 0, logicalCallContext, reqMcmMsg));
            }
            if ((MethodBase)RemotingProxy._getHashCodeMethod == methodBase)
            {
                return((IMessage) new ReturnMessage((object)identityObject.GetHashCode(), (object[])null, 0, logicalCallContext, reqMcmMsg));
            }
            if (identityObject.ChannelSink == null)
            {
                IMessageSink chnlSink  = (IMessageSink)null;
                IMessageSink envoySink = (IMessageSink)null;
                if (!identityObject.ObjectRef.IsObjRefLite())
                {
                    RemotingServices.CreateEnvoyAndChannelSinks((MarshalByRefObject)null, identityObject.ObjectRef, out chnlSink, out envoySink);
                }
                else
                {
                    RemotingServices.CreateEnvoyAndChannelSinks(identityObject.ObjURI, (object)null, out chnlSink, out envoySink);
                }
                RemotingServices.SetEnvoyAndChannelSinks(identityObject, chnlSink, envoySink);
                if (identityObject.ChannelSink == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_NoChannelSink"));
                }
            }
            IInternalMessage internalMessage = (IInternalMessage)reqMcmMsg;

            internalMessage.IdentityObject = identityObject;
            if (serverIdentity != null)
            {
                internalMessage.ServerIdentityObject = serverIdentity;
            }
            else
            {
                internalMessage.SetURI(identityObject.URI);
            }
            switch (callType)
            {
            case 0:
                bool         bSkippingContextChain  = false;
                Context      currentContextInternal = currentThread.GetCurrentContextInternal();
                IMessageSink ms = identityObject.EnvoyChain;
                if (currentContextInternal.IsDefaultContext && ms is EnvoyTerminatorSink)
                {
                    bSkippingContextChain = true;
                    ms = identityObject.ChannelSink;
                }
                message2 = RemotingProxy.CallProcessMessage(ms, (IMessage)reqMcmMsg, identityObject.ProxySideDynamicSinks, currentThread, currentContextInternal, bSkippingContextChain);
                break;

            case 1:
            case 9:
                LogicalCallContext callContext1 = (LogicalCallContext)logicalCallContext.Clone();
                internalMessage.SetCallContext(callContext1);
                AsyncResult asyncResult = new AsyncResult(message1);
                this.InternalInvokeAsync((IMessageSink)asyncResult, message1, useDispatchMessage, callType);
                message2 = (IMessage) new ReturnMessage((object)asyncResult, (object[])null, 0, (LogicalCallContext)null, (IMethodCallMessage)message1);
                break;

            case 2:
                message2 = RealProxy.EndInvokeHelper(message1, true);
                break;

            case 8:
                LogicalCallContext callContext2 = (LogicalCallContext)logicalCallContext.Clone();
                internalMessage.SetCallContext(callContext2);
                this.InternalInvokeAsync((IMessageSink)null, message1, useDispatchMessage, callType);
                message2 = (IMessage) new ReturnMessage((object)null, (object[])null, 0, (LogicalCallContext)null, reqMcmMsg);
                break;

            case 10:
                message2 = (IMessage) new ReturnMessage((object)null, (object[])null, 0, (LogicalCallContext)null, reqMcmMsg);
                break;
            }
            return(message2);
        }
예제 #7
0
        } // Invoke

        // This is called for all remoted calls on a TP except Ctors
        // The method called may be Sync, Async or OneWay(special case of Async)
        // In the Async case we come here for both BeginInvoke & EndInvoke
        internal virtual IMessage InternalInvoke(
            IMethodCallMessage reqMcmMsg, bool useDispatchMessage, int callType)
        {
            Message reqMsg = reqMcmMsg as Message;

            if ((reqMsg == null) && (callType != Message.Sync))
            {
                // Only the synchronous call type is supported for messages that
                //   aren't of type Message.
                throw new RemotingException(
                          Environment.GetResourceString("Remoting_Proxy_InvalidCallType"));
            }

            IMessage retMsg        = null;
            Thread   currentThread = Thread.CurrentThread;

            // pick up call context from the thread
            LogicalCallContext cctx = currentThread.GetLogicalCallContext();

            Identity       idObj    = IdentityObject;
            ServerIdentity serverID = idObj as ServerIdentity;

            if ((null != serverID) && idObj.IsFullyDisconnected())
            {
                throw new ArgumentException(
                          String.Format(Environment.GetResourceString("Remoting_ServerObjectNotFound"), reqMcmMsg.Uri));
            }

            // Short-circuit calls to Object::GetType and Object::GetHashCode
            MethodBase mb = reqMcmMsg.MethodBase;

            if (_getTypeMethod == mb)
            {
                // Time to load the true type of the remote object....
                Type t = GetProxiedType();
                return(new ReturnMessage(t, null, 0, null /*cctx*/, reqMcmMsg));
            }

            if (_getHashCodeMethod == mb)
            {
                int hashCode = idObj.GetHashCode();
                return(new ReturnMessage(hashCode, null, 0, null /*cctx*/, reqMcmMsg));
            }

            // check for channel sink
            if (idObj.ChannelSink == null)
            {
                throw new RemotingException(
                          Environment.GetResourceString("Remoting_Proxy_NoChannelSink"));
            }

            // Set the identity in the message object
            IInternalMessage iim = (IInternalMessage)reqMcmMsg;

            iim.IdentityObject = idObj;

            if (null != serverID)
            {
                Message.DebugOut("Setting serveridentity on message \n");
                iim.ServerIdentityObject = serverID;
            }
            else
            {
                // We need to set the URI only for identities that
                // are not the server identities. The uri is used to
                // dispatch methods for objects outside the appdomain.
                // Inside the appdomain (xcontext case) we dispatch
                // by getting the server object from the server identity.
                iim.SetURI(idObj.URI);
            }

            Message.DebugOut("InternalInvoke. Dispatching based on class type\n");
            AsyncResult ar = null;

            switch (callType)
            {
            case Message.Sync:
                Message.DebugOut("RemotingProxy.Invoke Call: SyncProcessMsg\n");
                BCLDebug.Assert(!useDispatchMessage, "!useDispatchMessage");
                bool         bSkipContextChain = false;
                Context      currentContext    = currentThread.GetCurrentContextInternal();
                IMessageSink nextSink          = idObj.EnvoyChain;

                // if we are in the default context, there can be no
                // client context chain, so we can skip the intermediate
                // calls if there are no envoy sinks

                if (currentContext.IsDefaultContext)
                {
                    if (nextSink is EnvoyTerminatorSink)
                    {
                        bSkipContextChain = true;

                        // jump directly to the channel sink
                        nextSink = idObj.ChannelSink;
                    }
                }

                retMsg = CallProcessMessage(nextSink,
                                            reqMcmMsg,
                                            idObj.ProxySideDynamicSinks,
                                            currentThread,
                                            currentContext,
                                            bSkipContextChain);

                break;

            case Message.BeginAsync:
            case Message.BeginAsync | Message.OneWay:
                // For async calls we clone the call context from the thread
                // This is a limited clone (we dont deep copy the user data)
                cctx = (LogicalCallContext)cctx.Clone();
                iim.SetCallContext(cctx);

                ar = new AsyncResult(reqMsg);

                InternalInvokeAsync(ar, reqMsg, useDispatchMessage, callType);

                Message.DebugOut("Propagate out params for BeginAsync\n");
                retMsg = new ReturnMessage(ar, null, 0, null /*cctx*/, reqMsg);
                break;

            case Message.OneWay:
                // For async calls we clone the call context from the thread
                // This is a limited clone (we dont deep copy the user data)
                cctx = (LogicalCallContext)cctx.Clone();
                iim.SetCallContext(cctx);
                InternalInvokeAsync(null, reqMsg, useDispatchMessage, callType);
                retMsg = new ReturnMessage(null, null, 0, null /*cctx*/, reqMcmMsg);
                break;

            case (Message.EndAsync | Message.OneWay):
                retMsg = new ReturnMessage(null, null, 0, null /*cctx*/, reqMcmMsg);
                break;

            case Message.EndAsync:
                // For endAsync, we merge back the returned callContext
                // into the thread's callContext
                retMsg = RealProxy.EndInvokeHelper(reqMsg, true);
                break;
            }

            return(retMsg);
        }
예제 #8
0
        internal virtual IMessage InternalInvoke(IMethodCallMessage reqMcmMsg, bool useDispatchMessage, int callType)
        {
            Message m = reqMcmMsg as Message;

            if ((m == null) && (callType != 0))
            {
                throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_InvalidCallType"));
            }
            IMessage           message2           = null;
            Thread             currentThread      = Thread.CurrentThread;
            LogicalCallContext logicalCallContext = currentThread.GetLogicalCallContext();
            Identity           identityObject     = this.IdentityObject;
            ServerIdentity     identity2          = identityObject as ServerIdentity;

            if ((identity2 != null) && identityObject.IsFullyDisconnected())
            {
                throw new ArgumentException(Environment.GetResourceString("Remoting_ServerObjectNotFound", new object[] { reqMcmMsg.Uri }));
            }
            MethodBase methodBase = reqMcmMsg.MethodBase;

            if (_getTypeMethod == methodBase)
            {
                return(new ReturnMessage(base.GetProxiedType(), null, 0, logicalCallContext, reqMcmMsg));
            }
            if (_getHashCodeMethod == methodBase)
            {
                return(new ReturnMessage(identityObject.GetHashCode(), null, 0, logicalCallContext, reqMcmMsg));
            }
            if (identityObject.ChannelSink == null)
            {
                IMessageSink chnlSink  = null;
                IMessageSink envoySink = null;
                if (!identityObject.ObjectRef.IsObjRefLite())
                {
                    RemotingServices.CreateEnvoyAndChannelSinks(null, identityObject.ObjectRef, out chnlSink, out envoySink);
                }
                else
                {
                    RemotingServices.CreateEnvoyAndChannelSinks(identityObject.ObjURI, null, out chnlSink, out envoySink);
                }
                RemotingServices.SetEnvoyAndChannelSinks(identityObject, chnlSink, envoySink);
                if (identityObject.ChannelSink == null)
                {
                    throw new RemotingException(Environment.GetResourceString("Remoting_Proxy_NoChannelSink"));
                }
            }
            IInternalMessage message3 = (IInternalMessage)reqMcmMsg;

            message3.IdentityObject = identityObject;
            if (identity2 != null)
            {
                message3.ServerIdentityObject = identity2;
            }
            else
            {
                message3.SetURI(identityObject.URI);
            }
            AsyncResult ar = null;

            switch (callType)
            {
            case 0:
            {
                bool         bSkippingContextChain  = false;
                Context      currentContextInternal = currentThread.GetCurrentContextInternal();
                IMessageSink envoyChain             = identityObject.EnvoyChain;
                if (currentContextInternal.IsDefaultContext && (envoyChain is EnvoyTerminatorSink))
                {
                    bSkippingContextChain = true;
                    envoyChain            = identityObject.ChannelSink;
                }
                return(CallProcessMessage(envoyChain, reqMcmMsg, identityObject.ProxySideDynamicSinks, currentThread, currentContextInternal, bSkippingContextChain));
            }

            case 1:
            case 9:
                logicalCallContext = (LogicalCallContext)logicalCallContext.Clone();
                message3.SetCallContext(logicalCallContext);
                ar = new AsyncResult(m);
                this.InternalInvokeAsync(ar, m, useDispatchMessage, callType);
                return(new ReturnMessage(ar, null, 0, null, m));

            case 2:
                return(RealProxy.EndInvokeHelper(m, true));

            case 3:
            case 4:
            case 5:
            case 6:
            case 7:
                return(message2);

            case 8:
                logicalCallContext = (LogicalCallContext)logicalCallContext.Clone();
                message3.SetCallContext(logicalCallContext);
                this.InternalInvokeAsync(null, m, useDispatchMessage, callType);
                return(new ReturnMessage(null, null, 0, null, reqMcmMsg));

            case 10:
                return(new ReturnMessage(null, null, 0, null, reqMcmMsg));
            }
            return(message2);
        }