Пример #1
0
        public virtual IMessage SyncProcessMessage(IMessage msg)
        {
            IMessage message = InternalSink.ValidateMessage(msg);

            if (message != null)
            {
                return(message);
            }
            IMethodCallMessage methodCallMessage   = msg as IMethodCallMessage;
            LogicalCallContext logicalCallContext1 = (LogicalCallContext)null;
            object             data = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext.GetData("__xADCall");
            bool     flag           = false;
            IMessage msg1;

            try
            {
                object obj = this._server;
                StackBuilderSink.VerifyIsOkToCallMethod(obj, (IMethodMessage)methodCallMessage);
                LogicalCallContext logicalCallContext2 = methodCallMessage == null ? (LogicalCallContext)msg.Properties[(object)"__CallContext"] : methodCallMessage.LogicalCallContext;
                logicalCallContext1 = CallContext.SetLogicalCallContext(logicalCallContext2);
                flag = true;
                logicalCallContext2.PropagateIncomingHeadersToCallContext(msg);
                StackBuilderSink.PreserveThreadPrincipalIfNecessary(logicalCallContext2, logicalCallContext1);
                if (this.IsOKToStackBlt((IMethodMessage)methodCallMessage, obj) && ((Message)methodCallMessage).Dispatch(obj))
                {
                    msg1 = (IMessage) new StackBasedReturnMessage();
                    ((StackBasedReturnMessage)msg1).InitFields((Message)methodCallMessage);
                    LogicalCallContext logicalCallContext3 = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;
                    logicalCallContext3.PropagateOutgoingHeadersToMessage(msg1);
                    ((StackBasedReturnMessage)msg1).SetLogicalCallContext(logicalCallContext3);
                }
                else
                {
                    MethodBase methodBase = StackBuilderSink.GetMethodBase((IMethodMessage)methodCallMessage);
                    object[]   outArgs1   = (object[])null;
                    RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(methodBase);
                    object[] args = Message.CoerceArgs((IMethodMessage)methodCallMessage, reflectionCachedData.Parameters);
                    object   ret  = this.PrivateProcessMessage(methodBase.MethodHandle, args, obj, out outArgs1);
                    this.CopyNonByrefOutArgsFromOriginalArgs(reflectionCachedData, args, ref outArgs1);
                    LogicalCallContext logicalCallContext3 = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;
                    if (data != null && (bool)data && logicalCallContext3 != null)
                    {
                        logicalCallContext3.RemovePrincipalIfNotSerializable();
                    }
                    object[]           outArgs2     = outArgs1;
                    int                outArgsCount = outArgs2 == null ? 0 : outArgs1.Length;
                    LogicalCallContext callCtx      = logicalCallContext3;
                    IMethodCallMessage mcm          = methodCallMessage;
                    msg1 = (IMessage) new ReturnMessage(ret, outArgs2, outArgsCount, callCtx, mcm);
                    logicalCallContext3.PropagateOutgoingHeadersToMessage(msg1);
                    CallContext.SetLogicalCallContext(logicalCallContext1);
                }
            }
            catch (Exception ex)
            {
                IMethodCallMessage mcm = methodCallMessage;
                msg1 = (IMessage) new ReturnMessage(ex, mcm);
                ((ReturnMessage)msg1).SetLogicalCallContext(methodCallMessage.LogicalCallContext);
                if (flag)
                {
                    CallContext.SetLogicalCallContext(logicalCallContext1);
                }
            }
            return(msg1);
        }
        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);
        }
Пример #3
0
        public virtual IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
        {
            IMethodCallMessage mcm1        = (IMethodCallMessage)msg;
            IMessageCtrl       messageCtrl = (IMessageCtrl)null;
            IMessage           msg1        = (IMessage)null;
            LogicalCallContext callCtx1    = (LogicalCallContext)null;
            bool flag = false;

            try
            {
                try
                {
                    LogicalCallContext logicalCallContext1 = (LogicalCallContext)mcm1.Properties[(object)Message.CallContextKey];
                    object             server = this._server;
                    StackBuilderSink.VerifyIsOkToCallMethod(server, (IMethodMessage)mcm1);
                    callCtx1 = CallContext.SetLogicalCallContext(logicalCallContext1);
                    flag     = true;
                    IMessage msg2 = msg;
                    logicalCallContext1.PropagateIncomingHeadersToCallContext(msg2);
                    LogicalCallContext threadCallContext = callCtx1;
                    StackBuilderSink.PreserveThreadPrincipalIfNecessary(logicalCallContext1, threadCallContext);
                    ServerChannelSinkStack channelSinkStack = msg.Properties[(object)"__SinkStack"] as ServerChannelSinkStack;
                    if (channelSinkStack != null)
                    {
                        channelSinkStack.ServerObject = server;
                    }
                    MethodBase methodBase = StackBuilderSink.GetMethodBase((IMethodMessage)mcm1);
                    object[]   outArgs1   = (object[])null;
                    RemotingMethodCachedData reflectionCachedData = InternalRemotingServices.GetReflectionCachedData(methodBase);
                    object[] args = Message.CoerceArgs((IMethodMessage)mcm1, reflectionCachedData.Parameters);
                    object   obj  = this.PrivateProcessMessage(methodBase.MethodHandle, args, server, out outArgs1);
                    this.CopyNonByrefOutArgsFromOriginalArgs(reflectionCachedData, args, ref outArgs1);
                    if (replySink != null)
                    {
                        LogicalCallContext logicalCallContext2 = Thread.CurrentThread.GetMutableExecutionContext().LogicalCallContext;
                        if (logicalCallContext2 != null)
                        {
                            logicalCallContext2.RemovePrincipalIfNotSerializable();
                        }
                        object             ret          = obj;
                        object[]           outArgs2     = outArgs1;
                        int                outArgsCount = outArgs2 == null ? 0 : outArgs1.Length;
                        LogicalCallContext callCtx2     = logicalCallContext2;
                        IMethodCallMessage mcm2         = mcm1;
                        msg1 = (IMessage) new ReturnMessage(ret, outArgs2, outArgsCount, callCtx2, mcm2);
                        logicalCallContext2.PropagateOutgoingHeadersToMessage(msg1);
                    }
                }
                catch (Exception ex)
                {
                    if (replySink != null)
                    {
                        msg1 = (IMessage) new ReturnMessage(ex, mcm1);
                        ((ReturnMessage)msg1).SetLogicalCallContext((LogicalCallContext)mcm1.Properties[(object)Message.CallContextKey]);
                    }
                }
                finally
                {
                    if (replySink != null)
                    {
                        replySink.SyncProcessMessage(msg1);
                    }
                }
            }
            finally
            {
                if (flag)
                {
                    CallContext.SetLogicalCallContext(callCtx1);
                }
            }
            return(messageCtrl);
        }
        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