示例#1
0
        internal async Task <MessageRpc> ProcessMessageAsync(MessageRpc rpc)
        {
            if (rpc.Operation.IsOneWay)
            {
                await rpc.RequestContext.ReplyAsync(null);

                rpc.OperationContext.RequestContext = null;
            }
            else
            {
                if (!rpc.Channel.IsReplyChannel &&
                    ((object)rpc.RequestID == null) &&
                    (rpc.Operation.Action != MessageHeaders.WildcardAction))
                {
                    CommunicationException error = new CommunicationException(SR.SFxOneWayMessageToTwoWayMethod0);
                    throw TraceUtility.ThrowHelperError(error, rpc.Request);
                }

                if (!manualAddressing)
                {
                    EndpointAddress replyTo = rpc.ReplyToInfo.ReplyTo;
                    if (replyTo != null && replyTo.IsNone && rpc.Channel.IsReplyChannel)
                    {
                        CommunicationException error = new CommunicationException(SR.SFxRequestReplyNone);
                        throw TraceUtility.ThrowHelperError(error, rpc.Request);
                    }

                    if (isOnServer)
                    {
                        EndpointAddress remoteAddress = rpc.Channel.RemoteAddress;
                        if ((remoteAddress != null) && !remoteAddress.IsAnonymous)
                        {
                            MessageHeaders headers   = rpc.Request.Headers;
                            Uri            remoteUri = remoteAddress.Uri;

                            if ((replyTo != null) && !replyTo.IsAnonymous && (remoteUri != replyTo.Uri))
                            {
                                string    text  = SR.Format(SR.SFxRequestHasInvalidReplyToOnServer, replyTo.Uri, remoteUri);
                                Exception error = new InvalidOperationException(text);
                                throw TraceUtility.ThrowHelperError(error, rpc.Request);
                            }

                            EndpointAddress faultTo = headers.FaultTo;
                            if ((faultTo != null) && !faultTo.IsAnonymous && (remoteUri != faultTo.Uri))
                            {
                                string    text  = SR.Format(SR.SFxRequestHasInvalidFaultToOnServer, faultTo.Uri, remoteUri);
                                Exception error = new InvalidOperationException(text);
                                throw TraceUtility.ThrowHelperError(error, rpc.Request);
                            }

                            if (rpc.RequestVersion.Addressing == AddressingVersion.WSAddressingAugust2004)
                            {
                                EndpointAddress from = headers.From;
                                if ((from != null) && !from.IsAnonymous && (remoteUri != from.Uri))
                                {
                                    string    text  = SR.Format(SR.SFxRequestHasInvalidFromOnServer, from.Uri, remoteUri);
                                    Exception error = new InvalidOperationException(text);
                                    throw TraceUtility.ThrowHelperError(error, rpc.Request);
                                }
                            }
                        }
                    }
                }
            }

            if (concurrency.IsConcurrent(ref rpc))
            {
                rpc.Channel.IncrementActivity();
                rpc.SuccessfullyIncrementedActivity = true;
            }

            //if (this.authenticationBehavior != null)
            //{
            //    this.authenticationBehavior.Authenticate(ref rpc);
            //}

            //if (this.authorizationBehavior != null)
            //{
            //    this.authorizationBehavior.Authorize(ref rpc);
            //}

            instance.EnsureInstanceContext(ref rpc);
            // TODO: Work out what function the pending list has and re-add/replace/remove functionality
            //TransferChannelFromPendingList(ref rpc);

            AcquireDynamicInstanceContext(ref rpc);

            AfterReceiveRequest(ref rpc);

            //if (!this.ignoreTransactionFlow)
            //{
            //    // Transactions need to have the context in the message
            //    rpc.TransactionMessageProperty = TransactionMessageProperty.TryGet(rpc.Request);
            //}

            rpc = await concurrency.LockInstanceAsync(rpc);

            rpc.SuccessfullyLockedInstance = true;

            // This also needs to happen after LockInstance, in case
            // we are using an AutoComplete=false transaction.
            //if (this.transaction != null)
            //{
            //    this.transaction.ResolveTransaction(ref rpc);
            //    if (rpc.Operation.TransactionRequired)
            //    {
            //        this.transaction.SetCurrent(ref rpc);
            //    }
            //}

            //rpc.NextProcessor = processMessage4;

            //if (this.transaction != null)
            //{
            //    if (rpc.Operation.TransactionRequired)
            //    {
            //        ReceiveContextRPCFacet receiveContext = rpc.ReceiveContext;

            //        if (receiveContext != null)
            //        {
            //            rpc.ReceiveContext = null;
            //            receiveContext.Complete(this, ref rpc, TimeSpan.MaxValue, rpc.Transaction.Current);
            //        }
            //    }
            //}
            //rpc.NextProcessor = processMessage41;

            try
            {
                // TaskHelpers has an extension method which enables awaitting a sync context to run continuation on it.
                await thread.GetSyncContext(rpc);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperFatal(e.Message, e);
            }

            // This needs to happen after LockInstance--LockInstance guarantees
            // in-order delivery, so we can't receive the next message until we
            // have acquired the lock.
            //
            // This also needs to happen after BindThread, since EricZ believes
            // that running on UI thread should guarantee in-order delivery if
            // the SynchronizationContext is single threaded.
            // Note: for IManualConcurrencyOperationInvoker, the invoke assumes full control over pumping.
            // TODO: This is the concurrency gate. If the service is concurrent, this allows another receive to happen. This mechanism needs replacing.
            //if (concurrency.IsConcurrent(ref rpc))
            //{
            //    rpc.EnsureReceive();
            //}

            instance.EnsureServiceInstance(ref rpc);

            try
            {
                //if (!rpc.Operation.IsSynchronous)
                //{
                //    // If async call completes in sync, it tells us through the gate below
                //    rpc.PrepareInvokeContinueGate();
                //}

                //if (this.transaction != null)
                //{
                //    this.transaction.InitializeCallContext(ref rpc);
                //}

                //SetActivityIdOnThread(ref rpc);

                rpc = await rpc.Operation.InvokeAsync(rpc);
            }
            catch
            {
                // This catch clause forces ClearCallContext to run prior to stackwalks exiting this frame.
                throw;
            }

            try
            {
                // Switch back to thread pool if we're using a non-default Sync Context
                await TaskHelpers.EnsureDefaultTaskScheduler();
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperFatal(e.Message, e);
            }

            try
            {
                error.ProvideMessageFault(ref rpc);
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

                error.HandleError(e);
            }

            PrepareReply(ref rpc);

            if (rpc.CanSendReply)
            {
                rpc.ReplyTimeoutHelper = new TimeoutHelper(rpc.Channel.OperationTimeout);
            }

            if (rpc.CanSendReply)
            {
                //if (rpc.Reply != null)
                //{
                //    TraceUtility.MessageFlowAtMessageSent(rpc.Reply, rpc.EventTraceActivity);
                //}

                rpc = await ReplyAsync(rpc);
            }

            // Logic for knowing when to close stuff:
            //
            // ASSUMPTIONS:
            //   Closing a stream over a message also closes the message.
            //   Closing a message over a stream does not close the stream.
            //     (OperationStreamProvider.ReleaseStream is no-op)
            //
            // This is a table of what should be disposed in what cases.
            // The rows represent the type of parameter to the method and
            // whether we are disposing parameters or not.  The columns
            // are for the inputs vs. the outputs.  The cells contain the
            // values that need to be Disposed.  M^P means that exactly
            // one of the message and parameter needs to be disposed,
            // since they refer to the same object.
            //
            //                               Request           Reply
            //               Message   |     M or P      |     M or P
            //     Dispose   Stream    |     P           |     M and P
            //               Params    |     M and P     |     M and P
            //                         |                 |
            //               Message   |     none        |     none
            //   NoDispose   Stream    |     none        |     M
            //               Params    |     M           |     M
            //
            // By choosing to dispose the parameter in both of the "M or P"
            // cases, the logic needed to generate this table is:
            //
            // CloseRequestMessage = IsParams
            // CloseRequestParams  = rpc.Operation.DisposeParameters
            // CloseReplyMessage   = rpc.Operation.SerializeReply
            // CloseReplyParams    = rpc.Operation.DisposeParameters
            //
            // IsParams can be calculated based on whether the request
            // message was consumed after deserializing but before calling
            // the user.  This is stored as rpc.DidDeserializeRequestBody.
            //
            Fx.Assert(
                !object.ReferenceEquals(rpc.ErrorProcessor, processMessageCleanupError),
                "ProcessMessageCleanup run twice on the same MessageRpc!");
            rpc.ErrorProcessor = processMessageCleanupError;

            bool replyWasSent = false;

            if (rpc.CanSendReply)
            {
                replyWasSent = rpc.SuccessfullySendReply;
            }

            try
            {
                try
                {
                    if (rpc.DidDeserializeRequestBody)
                    {
                        rpc.Request.Close();
                    }
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    error.HandleError(e);
                }

                rpc.DisposeParameters(false); //Dispose all input/output/return parameters

                if (rpc.FaultInfo.IsConsideredUnhandled)
                {
                    if (!replyWasSent)
                    {
                        rpc.AbortRequestContext();
                        rpc.AbortChannel();
                    }
                    else
                    {
                        rpc.CloseRequestContext();
                        rpc.CloseChannel();
                    }
                    rpc.AbortInstanceContext();
                }
                else
                {
                    if (rpc.RequestContextThrewOnReply)
                    {
                        rpc.AbortRequestContext();
                    }
                    else
                    {
                        rpc.CloseRequestContext();
                    }
                }


                if ((rpc.Reply != null) && (rpc.Reply != rpc.ReturnParameter))
                {
                    try
                    {
                        rpc.Reply.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        error.HandleError(e);
                    }
                }

                if ((rpc.FaultInfo.Fault != null) && (rpc.FaultInfo.Fault.State != MessageState.Closed))
                {
                    // maybe ProvideFault gave a Message, but then BeforeSendReply replaced it
                    // in that case, we need to close the one from ProvideFault
                    try
                    {
                        rpc.FaultInfo.Fault.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        error.HandleError(e);
                    }
                }

                try
                {
                    rpc.OperationContext.FireOperationCompleted();
                }

                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                }

                instance.AfterReply(ref rpc, error);

                if (rpc.SuccessfullyLockedInstance)
                {
                    try
                    {
                        concurrency.UnlockInstance(ref rpc);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }

                        Fx.Assert("Exceptions should be caught by callee");
                        rpc.InstanceContext.FaultInternal();
                        error.HandleError(e);
                    }
                }

                if (terminate != null)
                {
                    try
                    {
                        terminate.AfterReply(ref rpc);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        error.HandleError(e);
                    }
                }

                if (rpc.SuccessfullyIncrementedActivity)
                {
                    try
                    {
                        rpc.Channel.DecrementActivity();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        error.HandleError(e);
                    }
                }
            }
            finally
            {
                //if (rpc.MessageRpcOwnsInstanceContextThrottle && rpc.channelHandler.InstanceContextServiceThrottle != null)
                //{
                //    rpc.channelHandler.InstanceContextServiceThrottle.DeactivateInstanceContext();
                //}

                //if (rpc.Activity != null && DiagnosticUtility.ShouldUseActivity)
                //{
                //    rpc.Activity.Stop();
                //}
            }

            error.HandleError(ref rpc);
            return(rpc);
        }
        // Logic for knowing when to close stuff:
        //
        // ASSUMPTIONS:
        //   Closing a stream over a message also closes the message.
        //   Closing a message over a stream does not close the stream.
        //     (OperationStreamProvider.ReleaseStream is no-op)
        //
        // This is a table of what should be disposed in what cases.
        // The rows represent the type of parameter to the method and
        // whether we are disposing parameters or not.  The columns
        // are for the inputs vs. the outputs.  The cells contain the
        // values that need to be Disposed.  M^P means that exactly
        // one of the message and parameter needs to be disposed,
        // since they refer to the same object.
        //
        //                               Request           Reply
        //               Message   |     M or P      |     M or P
        //     Dispose   Stream    |     P           |     M and P
        //               Params    |     M and P     |     M and P
        //                         |                 |
        //               Message   |     none        |     none
        //   NoDispose   Stream    |     none        |     M
        //               Params    |     M           |     M
        //
        // By choosing to dispose the parameter in both of the "M or P"
        // cases, the logic needed to generate this table is:
        //
        // CloseRequestMessage = IsParams
        // CloseRequestParams  = rpc.Operation.DisposeParameters
        // CloseReplyMessage   = rpc.Operation.SerializeReply
        // CloseReplyParams    = rpc.Operation.DisposeParameters
        //
        // IsParams can be calculated based on whether the request
        // message was consumed after deserializing but before calling
        // the user.  This is stored as rpc.DidDeserializeRequestBody.
        //
        void ProcessMessageCleanup(MessageRpc rpc)
        {
            Fx.Assert(
                !object.ReferenceEquals(rpc.ErrorProcessor, _processMessageCleanupError),
                "ProcessMessageCleanup run twice on the same MessageRpc!");
            rpc.ErrorProcessor = _processMessageCleanupError;

            bool replyWasSent = false;

            if (rpc.CanSendReply)
            {
                replyWasSent = rpc.SuccessfullySendReply;
            }

            try
            {
                try
                {
                    if (rpc.DidDeserializeRequestBody)
                    {
                        rpc.Request.Close();
                    }
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    _error.HandleError(e);
                }

                rpc.DisposeParameters(false); //Dispose all input/output/return parameters

                if (rpc.FaultInfo.IsConsideredUnhandled)
                {
                    if (!replyWasSent)
                    {
                        rpc.AbortRequestContext();
                        rpc.AbortChannel();
                    }
                    else
                    {
                        rpc.CloseRequestContext();
                        rpc.CloseChannel();
                    }
                    rpc.AbortInstanceContext();
                }
                else
                {
                    if (rpc.RequestContextThrewOnReply)
                    {
                        rpc.AbortRequestContext();
                    }
                    else
                    {
                        rpc.CloseRequestContext();
                    }
                }

                if ((rpc.Reply != null) && (rpc.Reply != rpc.ReturnParameter))
                {
                    try
                    {
                        rpc.Reply.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        _error.HandleError(e);
                    }
                }

                if ((rpc.FaultInfo.Fault != null) && (rpc.FaultInfo.Fault.State != MessageState.Closed))
                {
                    // maybe ProvideFault gave a Message, but then BeforeSendReply replaced it
                    // in that case, we need to close the one from ProvideFault
                    try
                    {
                        rpc.FaultInfo.Fault.Close();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        _error.HandleError(e);
                    }
                }

                try
                {
                    rpc.OperationContext.FireOperationCompleted();
                }

                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                }

                InstanceBehavior.AfterReply(ref rpc, _error);

                if (rpc.SuccessfullyLockedInstance)
                {
                    try
                    {
                        _concurrency.UnlockInstance(ref rpc);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }

                        Fx.Assert("Exceptions should be caught by callee");
                        rpc.InstanceContext.FaultInternal();
                        _error.HandleError(e);
                    }
                }

                if (_terminate != null)
                {
                    try
                    {
                        _terminate.AfterReply(ref rpc);
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        _error.HandleError(e);
                    }
                }

                if (rpc.SuccessfullyIncrementedActivity)
                {
                    try
                    {
                        rpc.Channel.DecrementActivity();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        _error.HandleError(e);
                    }
                }
            }
            finally
            {
                // TODO: Add the code for the other half of InstanceContextServiceThrottle being acquired
                if (rpc.MessageRpcOwnsInstanceContextThrottle && rpc.channelHandler.InstanceContextServiceThrottle != null)
                {
                    rpc.channelHandler.InstanceContextServiceThrottle.DeactivateInstanceContext();
                }

                //if (rpc.Activity != null && DiagnosticUtility.ShouldUseActivity)
                //{
                //    rpc.Activity.Stop();
                //}
            }

            _error.HandleError(rpc);
        }