Ejemplo n.º 1
0
        protected override void Execute(NativeActivityContext context)
        {
            MessageVersion version;

            SendReceiveExtension sendReceiveExtension = context.GetExtension <SendReceiveExtension>();

            if (sendReceiveExtension != null)
            {
                HostSettings hostSettings = sendReceiveExtension.HostSettings;
                this.IncludeExceptionDetailInFaults = hostSettings.IncludeExceptionDetailInFaults;
            }

            CorrelationHandle correlatesWith = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(context);

            if (correlatesWith == null)
            {
                correlatesWith = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
            }

            CorrelationResponseContext responseContext;

            if (correlatesWith != null)
            {
                if (sendReceiveExtension != null)
                {
                    if (!this.TryGetMessageVersion(correlatesWith.InstanceKey, out version))
                    {
                        throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.MessageVersionInformationNotFound));
                    }
                }
                else if (correlatesWith.TryAcquireResponseContext(context, out responseContext))
                {
                    //Register the ResponseContext so that InternalSendMessage can access it.
                    if (!correlatesWith.TryRegisterResponseContext(context, responseContext))
                    {
                        throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.ResponseContextIsNotNull));
                    }

                    //Use the same MessageVersion as the incoming message that is retrieved using CorrelatonHandle
                    version = responseContext.MessageVersion;
                }
                else
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CorrelationResponseContextShouldNotBeNull));
                }
            }
            else
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CorrelationResponseContextShouldNotBeNull));
            }

            Fx.Assert((this.Formatter == null && this.FaultFormatter != null) ||
                      (this.Formatter != null && this.FaultFormatter == null),
                      "OperationFormatter and FaultFormatter cannot be both null or both set!");

            if (this.FaultFormatter != null)
            {
                Fx.Assert(this.parameters.Count == 1, "Exception should be the only parameter!");
                Exception exception = this.parameters[0].Get(context) as Exception;
                Fx.Assert(exception != null, "InArgument must be an Exception!");

                MessageFault messageFault;
                string       action;

                FaultException faultException = exception as FaultException;
                if (faultException != null)
                {
                    // This is an expected fault
                    // Reproduce logic from ErrorBehavior.InitializeFault

                    messageFault = this.FaultFormatter.Serialize(faultException, out action);
                    if (action == null)
                    {
                        action = version.Addressing.DefaultFaultAction;
                    }
                }
                else
                {
                    // This is an unexpected fault
                    // Reproduce logic from ErrorBehavior.ProvideFaultOfLastResort

                    FaultCode code = new FaultCode(FaultCodeConstants.Codes.InternalServiceFault, FaultCodeConstants.Namespaces.NetDispatch);
                    code = FaultCode.CreateReceiverFaultCode(code);

                    action = FaultCodeConstants.Actions.NetDispatcher;

                    if (this.IncludeExceptionDetailInFaults)
                    {
                        messageFault = MessageFault.CreateFault(code,
                                                                new FaultReason(new FaultReasonText(exception.Message, CultureInfo.CurrentCulture)),
                                                                new ExceptionDetail(exception));
                    }
                    else
                    {
                        messageFault = MessageFault.CreateFault(code,
                                                                new FaultReason(new FaultReasonText(SR2.InternalServerError, CultureInfo.CurrentCulture)));
                    }
                }

                if (messageFault == null)
                {
                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.CannotCreateMessageFault));
                }
                else
                {
                    Message outMessage = System.ServiceModel.Channels.Message.CreateMessage(version, messageFault, action);
                    this.Message.Set(context, outMessage);
                }
            }
            else
            {
                object[] inObjects;
                if (this.parameters != null)
                {
                    inObjects = new object[this.parameters.Count];
                    for (int i = 0; i < this.parameters.Count; i++)
                    {
                        Fx.Assert(this.Parameters[i] != null, "Parameter cannot be null");
                        inObjects[i] = this.parameters[i].Get(context);
                    }
                }
                else
                {
                    inObjects = Constants.EmptyArray;
                }

                object returnValue = null;
                if (this.Result != null)
                {
                    returnValue = this.Result.Get(context);
                }

                Message outMessage = this.Formatter.SerializeReply(version, inObjects, returnValue);
                this.Message.Set(context, outMessage);
            }
        }
Ejemplo n.º 2
0
        protected override void Execute(NativeActivityContext context)
        {
            CorrelationResponseContext context2;
            CorrelationHandle          handle = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(context);

            if (handle == null)
            {
                handle = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
            }
            if ((handle == null) || !handle.TryAcquireResponseContext(context, out context2))
            {
                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.CorrelationResponseContextShouldNotBeNull));
            }
            if (!handle.TryRegisterResponseContext(context, context2))
            {
                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.ResponseContextIsNotNull));
            }
            MessageVersion messageVersion = context2.MessageVersion;

            if (this.FaultFormatter != null)
            {
                MessageFault   fault;
                string         defaultFaultAction;
                Exception      exception      = this.parameters[0].Get(context) as Exception;
                FaultException faultException = exception as FaultException;
                if (faultException != null)
                {
                    fault = this.FaultFormatter.Serialize(faultException, out defaultFaultAction);
                    if (defaultFaultAction == null)
                    {
                        defaultFaultAction = messageVersion.Addressing.DefaultFaultAction;
                    }
                }
                else
                {
                    FaultCode subCode = new FaultCode("InternalServiceFault", "http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher");
                    subCode            = FaultCode.CreateReceiverFaultCode(subCode);
                    defaultFaultAction = "http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher/fault";
                    if (this.IncludeExceptionDetailInFaults)
                    {
                        fault = MessageFault.CreateFault(subCode, new FaultReason(new FaultReasonText(exception.Message, CultureInfo.CurrentCulture)), new ExceptionDetail(exception));
                    }
                    else
                    {
                        fault = MessageFault.CreateFault(subCode, new FaultReason(new FaultReasonText(System.ServiceModel.Activities.SR.InternalServerError, CultureInfo.CurrentCulture)));
                    }
                }
                if (fault == null)
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.CannotCreateMessageFault));
                }
                System.ServiceModel.Channels.Message message = System.ServiceModel.Channels.Message.CreateMessage(messageVersion, fault, defaultFaultAction);
                this.Message.Set(context, message);
            }
            else
            {
                object[] emptyArray;
                if (this.parameters != null)
                {
                    emptyArray = new object[this.parameters.Count];
                    for (int i = 0; i < this.parameters.Count; i++)
                    {
                        emptyArray[i] = this.parameters[i].Get(context);
                    }
                }
                else
                {
                    emptyArray = System.ServiceModel.Activities.Constants.EmptyArray;
                }
                object result = null;
                if (this.Result != null)
                {
                    result = this.Result.Get(context);
                }
                System.ServiceModel.Channels.Message message2 = this.Formatter.SerializeReply(messageVersion, emptyArray, result);
                this.Message.Set(context, message2);
            }
        }