GetLogicalCallContext() static private méthode

static private GetLogicalCallContext ( ) : LogicalCallContext
Résultat LogicalCallContext
        public virtual IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            IMethodCallMessage message           = (IMethodCallMessage)msg;
            IMessageCtrl       ctrl              = null;
            IMessage           message2          = null;
            LogicalCallContext threadCallContext = null;
            bool flag = false;

            try
            {
                try
                {
                    try
                    {
                        LogicalCallContext callCtx = (LogicalCallContext)message.Properties[Message.CallContextKey];
                        object             server  = this._server;
                        VerifyIsOkToCallMethod(server, message);
                        threadCallContext = CallContext.SetLogicalCallContext(callCtx);
                        flag = true;
                        callCtx.PropagateIncomingHeadersToCallContext(msg);
                        PreserveThreadPrincipalIfNecessary(callCtx, threadCallContext);
                        ServerChannelSinkStack stack = msg.Properties["__SinkStack"] as ServerChannelSinkStack;
                        if (stack != null)
                        {
                            stack.ServerObject = server;
                        }
                        MethodBase methodBase = GetMethodBase(message);
                        object[]   outArgs    = null;
                        object     ret        = null;
                        RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(methodBase);
                        object[] args = Message.CoerceArgs(message, reflectionCachedData.Parameters);
                        ret = this.PrivateProcessMessage(methodBase.MethodHandle, args, server, 0, false, out outArgs);
                        this.CopyNonByrefOutArgsFromOriginalArgs(reflectionCachedData, args, ref outArgs);
                        if (replySink != null)
                        {
                            LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
                            if (logicalCallContext != null)
                            {
                                logicalCallContext.RemovePrincipalIfNotSerializable();
                            }
                            message2 = new ReturnMessage(ret, outArgs, (outArgs == null) ? 0 : outArgs.Length, logicalCallContext, message);
                            logicalCallContext.PropagateOutgoingHeadersToMessage(message2);
                        }
                        return(ctrl);
                    }
                    catch (Exception exception)
                    {
                        if (replySink != null)
                        {
                            message2 = new ReturnMessage(exception, message);
                            ((ReturnMessage)message2).SetLogicalCallContext((LogicalCallContext)message.Properties[Message.CallContextKey]);
                        }
                    }
                    return(ctrl);
                }
                finally
                {
                    if (replySink != null)
                    {
                        replySink.SyncProcessMessage(message2);
                    }
                }
            }
            finally
            {
                if (flag)
                {
                    CallContext.SetLogicalCallContext(threadCallContext);
                }
            }
            return(ctrl);
        }
        internal virtual IMessage SyncProcessMessage(IMessage msg, int methodPtr, bool fExecuteInContext)
        {
            // Validate message here
            IMessage errMsg = InternalSink.ValidateMessage(msg);

            if (errMsg != null)
            {
                return(errMsg);
            }

            IMethodCallMessage mcMsg = msg as IMethodCallMessage;

            IMessage           retMessage;
            LogicalCallContext oldCallCtx = null;

            LogicalCallContext lcc     = CallContext.GetLogicalCallContext();
            object             xADCall = lcc.GetData(CrossAppDomainSink.LCC_DATA_KEY);

            bool isCallContextSet = false;

            try
            {
                Object server = _server;

                BCLDebug.Assert((server != null) == (!_bStatic),
                                "Invalid state in stackbuilder sink?");

                // validate the method base if necessary
                VerifyIsOkToCallMethod(server, mcMsg);

                // install call context onto the thread, holding onto
                // the one that is currently on the thread

                LogicalCallContext messageCallContext = null;
                if (mcMsg != null)
                {
                    messageCallContext = mcMsg.LogicalCallContext;
                }
                else
                {
                    messageCallContext = (LogicalCallContext)msg.Properties["__CallContext"];
                }

                oldCallCtx       = CallContext.SetLogicalCallContext(messageCallContext);
                isCallContextSet = true;

                messageCallContext.PropagateIncomingHeadersToCallContext(msg);

                PreserveThreadPrincipalIfNecessary(messageCallContext, oldCallCtx);


                // NOTE: target for dispatch will be NULL when the StackBuilderSink
                // is used for async delegates on static methods.

                // *** NOTE ***
                // Although we always pass _server to these calls in the EE,
                // when we execute using Message::Dispatch we are using TP as
                // the this-ptr ... (what the call site thinks is the this-ptr)
                // when we execute using StackBuilderSink::PrivatePM we use
                // _server as the this-ptr (which could be different if there
                // is interception for strictly MBR types in the same AD).
                // ************
                if (IsOKToStackBlt(mcMsg, server) &&
                    ((Message)mcMsg).Dispatch(server, fExecuteInContext))
                {
                    //retMessage = StackBasedReturnMessage.GetObjectFromPool((Message)mcMsg);
                    retMessage = new StackBasedReturnMessage();
                    ((StackBasedReturnMessage)retMessage).InitFields((Message)mcMsg);

                    // call context could be different then the one from before the call.
                    LogicalCallContext latestCallContext = CallContext.GetLogicalCallContext();
                    // retrieve outgoing response headers
                    latestCallContext.PropagateOutgoingHeadersToMessage(retMessage);

                    // Install call context back into Message (from the thread)
                    ((StackBasedReturnMessage)retMessage).SetLogicalCallContext(latestCallContext);
                }
                else
                {
                    MethodBase mb      = GetMethodBase(mcMsg);
                    Object[]   outArgs = null;
                    Object     ret     = null;

                    RemotingMethodCachedData methodCache =
                        InternalRemotingServices.GetReflectionCachedData(mb);

                    Message.DebugOut("StackBuilderSink::Calling PrivateProcessMessage\n");

                    Object[] args = Message.CoerceArgs(mcMsg, methodCache.Parameters);

                    ret = PrivateProcessMessage(
                        mb.MethodHandle,
                        args,
                        server,
                        methodPtr,
                        fExecuteInContext,
                        out outArgs);
                    CopyNonByrefOutArgsFromOriginalArgs(methodCache, args, ref outArgs);


                    // call context could be different then the one from before the call.
                    LogicalCallContext latestCallContext = CallContext.GetLogicalCallContext();

                    if (xADCall != null && ((bool)xADCall) == true && latestCallContext != null)
                    {
                        // Special case Principal since if might not be serializable before returning
                        // ReturnMessage
                        latestCallContext.RemovePrincipalIfNotSerializable();
                    }

                    retMessage = new ReturnMessage(
                        ret,
                        outArgs,
                        (outArgs == null ? 0 : outArgs.Length),
                        latestCallContext,
                        mcMsg);

                    // retrieve outgoing response headers
                    latestCallContext.PropagateOutgoingHeadersToMessage(retMessage);
                    // restore the call context on the thread
                    CallContext.SetLogicalCallContext(oldCallCtx);
                }
            } catch (Exception e)
            {
                Message.DebugOut(
                    "StackBuilderSink::The server object probably threw an exception " +
                    e.Message + e.StackTrace + "\n");
                retMessage = new ReturnMessage(e, mcMsg);
                ((ReturnMessage)retMessage).SetLogicalCallContext(mcMsg.LogicalCallContext);

                if (isCallContextSet)
                {
                    CallContext.SetLogicalCallContext(oldCallCtx);
                }
            }


            return(retMessage);
        }
        internal virtual IMessage SyncProcessMessage(IMessage msg, int methodPtr, bool fExecuteInContext)
        {
            IMessage message3;
            IMessage message = InternalSink.ValidateMessage(msg);

            if (message != null)
            {
                return(message);
            }
            IMethodCallMessage message2          = msg as IMethodCallMessage;
            LogicalCallContext threadCallContext = null;
            object             obj2 = CallContext.GetLogicalCallContext().GetData("__xADCall");
            bool flag = false;

            try
            {
                object server = this._server;
                VerifyIsOkToCallMethod(server, message2);
                LogicalCallContext callCtx = null;
                if (message2 != null)
                {
                    callCtx = message2.LogicalCallContext;
                }
                else
                {
                    callCtx = (LogicalCallContext)msg.Properties["__CallContext"];
                }
                threadCallContext = CallContext.SetLogicalCallContext(callCtx);
                flag = true;
                callCtx.PropagateIncomingHeadersToCallContext(msg);
                PreserveThreadPrincipalIfNecessary(callCtx, threadCallContext);
                if (this.IsOKToStackBlt(message2, server) && ((Message)message2).Dispatch(server, fExecuteInContext))
                {
                    message3 = new StackBasedReturnMessage();
                    ((StackBasedReturnMessage)message3).InitFields((Message)message2);
                    LogicalCallContext context4 = CallContext.GetLogicalCallContext();
                    context4.PropagateOutgoingHeadersToMessage(message3);
                    ((StackBasedReturnMessage)message3).SetLogicalCallContext(context4);
                    return(message3);
                }
                MethodBase methodBase = GetMethodBase(message2);
                object[]   outArgs    = null;
                object     ret        = null;
                RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(methodBase);
                object[] args = Message.CoerceArgs(message2, reflectionCachedData.Parameters);
                ret = this.PrivateProcessMessage(methodBase.MethodHandle, args, server, methodPtr, fExecuteInContext, out outArgs);
                this.CopyNonByrefOutArgsFromOriginalArgs(reflectionCachedData, args, ref outArgs);
                LogicalCallContext logicalCallContext = CallContext.GetLogicalCallContext();
                if (((obj2 != null) && ((bool)obj2)) && (logicalCallContext != null))
                {
                    logicalCallContext.RemovePrincipalIfNotSerializable();
                }
                message3 = new ReturnMessage(ret, outArgs, (outArgs == null) ? 0 : outArgs.Length, logicalCallContext, message2);
                logicalCallContext.PropagateOutgoingHeadersToMessage(message3);
                CallContext.SetLogicalCallContext(threadCallContext);
            }
            catch (Exception exception)
            {
                message3 = new ReturnMessage(exception, message2);
                ((ReturnMessage)message3).SetLogicalCallContext(message2.LogicalCallContext);
                if (flag)
                {
                    CallContext.SetLogicalCallContext(threadCallContext);
                }
            }
            return(message3);
        }
        public virtual IMessageCtrl AsyncProcessMessage(
            IMessage msg, IMessageSink replySink)
        {
            IMethodCallMessage mcMsg = (IMethodCallMessage)msg;

            IMessageCtrl       retCtrl    = null;
            IMessage           retMessage = null;
            LogicalCallContext oldCallCtx = null;
            bool isCallContextSet         = false;

            try{
                try
                {
                    LogicalCallContext callCtx = (LogicalCallContext)
                                                 mcMsg.Properties[Message.CallContextKey];

                    Object server = _server;

                    // validate the method base if necessary
                    VerifyIsOkToCallMethod(server, mcMsg);

                    // install call context onto the thread, holding onto
                    // the one that is currently on the thread

                    oldCallCtx       = CallContext.SetLogicalCallContext(callCtx);
                    isCallContextSet = true;
                    // retrieve incoming headers
                    callCtx.PropagateIncomingHeadersToCallContext(msg);

                    PreserveThreadPrincipalIfNecessary(callCtx, oldCallCtx);

                    // see if this is a server message that was dispatched asynchronously
                    ServerChannelSinkStack sinkStack =
                        msg.Properties["__SinkStack"] as ServerChannelSinkStack;
                    if (sinkStack != null)
                    {
                        sinkStack.ServerObject = server;
                    }

                    BCLDebug.Assert((server != null) == (!_bStatic),
                                    "Invalid state in stackbuilder sink?");

                    MethodBase mb      = GetMethodBase(mcMsg);
                    Object[]   outArgs = null;
                    Object     ret     = null;
                    RemotingMethodCachedData methodCache =
                        InternalRemotingServices.GetReflectionCachedData(mb);
                    Object[] args = Message.CoerceArgs(mcMsg, methodCache.Parameters);

                    ret = PrivateProcessMessage(mb.MethodHandle,
                                                args,
                                                server,
                                                0,
                                                false,
                                                out outArgs);
                    CopyNonByrefOutArgsFromOriginalArgs(methodCache, args, ref outArgs);

                    if (replySink != null)
                    {
                        // call context could be different then the one from before the call.
                        LogicalCallContext latestCallContext = CallContext.GetLogicalCallContext();

                        if (latestCallContext != null)
                        {
                            // Special case Principal since if might not be serializable before returning
                            // ReturnMessage
                            latestCallContext.RemovePrincipalIfNotSerializable();
                        }

                        retMessage = new ReturnMessage(
                            ret,
                            outArgs,
                            (outArgs == null ? 0 : outArgs.Length),
                            latestCallContext,
                            mcMsg);

                        // retrieve outgoing response headers
                        latestCallContext.PropagateOutgoingHeadersToMessage(retMessage);
                    }
                }
                catch (Exception e)
                {
                    if (replySink != null)
                    {
                        retMessage = new ReturnMessage(e, mcMsg);
                        ((ReturnMessage)retMessage).SetLogicalCallContext(
                            (LogicalCallContext)
                            mcMsg.Properties[Message.CallContextKey]);
                    }
                }
                finally
                {
                    // Call the reply sink without catching the exceptions
                    // in it. In v2.0 any exceptions in the callback for example
                    // would probably bring down the process
                    replySink.SyncProcessMessage(retMessage);
                }
            }
            finally {
                // restore the call context on the thread
                if (isCallContextSet)
                {
                    CallContext.SetLogicalCallContext(oldCallCtx);
                }
            }

            return(retCtrl);
        } // AsyncProcessMessage