コード例 #1
0
ファイル: ChannelHandler.cs プロジェクト: srm2020dev/CoreWCF
        private async Task ReplyFailureAsync(RequestContext request, RequestInfo requestInfo, Message fault, string action, string reason, FaultCode code)
        {
            FaultException exception = new FaultException(reason, code);

            ErrorBehavior.ThrowAndCatch(exception);
            ErrorHandlerFaultInfo faultInfo = new ErrorHandlerFaultInfo(action);

            faultInfo.Fault = fault;
            faultInfo       = await ProvideFaultAndReplyFailureAsync(request, requestInfo, exception, faultInfo);

            HandleError(exception, ref faultInfo);
        }
コード例 #2
0
ファイル: DispatchRuntime.cs プロジェクト: suneths/CoreWCF
            public object Invoke(object instance, object[] inputs, out object[] outputs)
            {
                outputs = EmptyArray <object> .Allocate(0);

                Message message = inputs[0] as Message;

                if (message == null)
                {
                    return(null);
                }

                string action = message.Headers.Action;

                //if (DiagnosticUtility.ShouldTraceInformation)
                //{
                //    TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.UnhandledAction,
                //        SR.TraceCodeUnhandledAction,
                //        new StringTraceRecord("Action", action),
                //        this, null, message);
                //}

                FaultCode code = FaultCode.CreateSenderFaultCode(AddressingStrings.ActionNotSupported,
                                                                 message.Version.Addressing.Namespace);
                string      reasonText = SR.Format(SR.SFxNoEndpointMatchingContract, action);
                FaultReason reason     = new FaultReason(reasonText);

                FaultException exception = new FaultException(reason, code);

                ErrorBehavior.ThrowAndCatch(exception);

                ServiceChannel serviceChannel = OperationContext.Current.InternalServiceChannel;

                OperationContext.Current.OperationCompleted +=
                    delegate(object sender, EventArgs e)
                {
                    ChannelDispatcher channelDispatcher = dispatchRuntime.ChannelDispatcher;
                    if (!channelDispatcher.HandleError(exception) && serviceChannel.HasSession)
                    {
                        try
                        {
                            var helper = new TimeoutHelper(ChannelHandler.CloseAfterFaultTimeout);
                            serviceChannel.CloseAsync(helper.GetCancellationToken()).GetAwaiter().GetResult();
                        }
                        catch (Exception ex)
                        {
                            if (Fx.IsFatal(ex))
                            {
                                throw;
                            }
                            channelDispatcher.HandleError(ex);
                        }
                    }
                };

                if (dispatchRuntime.shared.EnableFaults)
                {
                    MessageFault fault = MessageFault.CreateFault(code, reason, action);
                    return(Message.CreateMessage(message.Version, fault, message.Version.Addressing.DefaultFaultAction));
                }
                else
                {
                    OperationContext.Current.RequestContext.CloseAsync().GetAwaiter().GetResult();
                    OperationContext.Current.RequestContext = null;
                    return(null);
                }
            }
コード例 #3
0
        void SerializeOutputs(ref MessageRpc rpc)
        {
            if (!IsOneWay && parent.EnableFaults)
            {
                Message reply;
                if (serializeReply)
                {
                    try
                    {
                        //if (TD.DispatchFormatterSerializeReplyStartIsEnabled())
                        //{
                        //    TD.DispatchFormatterSerializeReplyStart(rpc.EventTraceActivity);
                        //}

                        reply = Formatter.SerializeReply(rpc.RequestVersion, rpc.OutputParameters, rpc.ReturnParameter);

                        //if (TD.DispatchFormatterSerializeReplyStopIsEnabled())
                        //{
                        //    TD.DispatchFormatterSerializeReplyStop(rpc.EventTraceActivity);
                        //}
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }
                        if (ErrorBehavior.ShouldRethrowExceptionAsIs(e))
                        {
                            throw;
                        }
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperCallback(e);
                    }

                    if (reply == null)
                    {
                        string message = SR.Format(SR.SFxNullReplyFromFormatter2, Formatter.GetType().ToString(), (name ?? ""));
                        ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message));
                    }
                }
                else
                {
                    if ((rpc.ReturnParameter == null) && (rpc.OperationContext.RequestContext != null))
                    {
                        string message = SR.Format(SR.SFxDispatchRuntimeMessageCannotBeNull, name);
                        ErrorBehavior.ThrowAndCatch(new InvalidOperationException(message));
                    }

                    reply = (Message)rpc.ReturnParameter;

                    if ((reply != null) && (!ProxyOperationRuntime.IsValidAction(reply, ReplyAction)))
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.SFxInvalidReplyAction, Name, reply.Headers.Action ?? "{NULL}", ReplyAction)));
                    }
                }

                //if (DiagnosticUtility.ShouldUseActivity && rpc.Activity != null && reply != null)
                //{
                //    TraceUtility.SetActivity(reply, rpc.Activity);
                //    if (TraceUtility.ShouldPropagateActivity)
                //    {
                //        TraceUtility.AddActivityHeader(reply);
                //    }
                //}
                //else if (TraceUtility.ShouldPropagateActivity && reply != null && rpc.ResponseActivityId != Guid.Empty)
                //{
                //    ActivityIdHeader header = new ActivityIdHeader(rpc.ResponseActivityId);
                //    header.AddTo(reply);
                //}

                //rely on the property set during the message receive to correlate the trace
                //if (TraceUtility.MessageFlowTracingOnly)
                //{
                //    //Guard against MEX scenarios where the message is closed by now
                //    if (null != rpc.OperationContext.IncomingMessage && MessageState.Closed != rpc.OperationContext.IncomingMessage.State)
                //    {
                //        FxTrace.Trace.SetAndTraceTransfer(TraceUtility.GetReceivedActivityId(rpc.OperationContext), true);
                //    }
                //    else
                //    {
                //        if (rpc.ResponseActivityId != Guid.Empty)
                //        {
                //            FxTrace.Trace.SetAndTraceTransfer(rpc.ResponseActivityId, true);
                //        }
                //    }
                //}

                // Add the ImpersonateOnSerializingReplyMessageProperty on the reply message iff
                // a. reply message is not null.
                // b. Impersonation is enabled on serializing Reply

                //if (reply != null && this.parent.IsImpersonationEnabledOnSerializingReply)
                //{
                //    bool shouldImpersonate = this.parent.SecurityImpersonation != null && this.parent.SecurityImpersonation.IsImpersonationEnabledOnCurrentOperation(ref rpc);
                //    if (shouldImpersonate)
                //    {
                //        reply.Properties.Add(ImpersonateOnSerializingReplyMessageProperty.Name, new ImpersonateOnSerializingReplyMessageProperty(ref rpc));
                //        reply = new ImpersonatingMessage(reply);
                //    }
                //}

                //if (MessageLogger.LoggingEnabled && null != reply)
                //{
                //    MessageLogger.LogMessage(ref reply, MessageLoggingSource.ServiceLevelSendReply | MessageLoggingSource.LastChance);
                //}
                rpc.Reply = reply;
            }
        }