protected override void Execute(NativeActivityContext context)
        {
            CorrelationHandle handle = (this.Correlation == null) ? null : this.Correlation.Get(context);

            if (handle == null)
            {
                handle = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
                if (handle == null)
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.NullCorrelationHandleInInitializeCorrelation(base.DisplayName)));
                }
            }
            CorrelationExtension extension = context.GetExtension <CorrelationExtension>();

            if (extension == null)
            {
                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.InitializeCorrelationRequiresWorkflowServiceHost(base.DisplayName)));
            }
            Dictionary <string, string> keyData = new Dictionary <string, string>();

            foreach (KeyValuePair <string, InArgument <string> > pair in this.CorrelationData)
            {
                keyData.Add(pair.Key, pair.Value.Get(context));
            }
            handle.InitializeBookmarkScope(context, extension.GenerateKey(keyData));
        }
        protected override void Execute(NativeActivityContext context)
        {
            CorrelationHandle correlationHandle = (this.Correlation == null) ? null : this.Correlation.Get(context);

            if (correlationHandle == null)
            {
                //throw only if ambient correlation handle is also null
                correlationHandle = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
                if (correlationHandle == null)
                {
                    throw FxTrace.Exception.AsError(
                              new InvalidOperationException(SR2.NullCorrelationHandleInInitializeCorrelation(this.DisplayName)));
                }
            }

            CorrelationExtension extension = context.GetExtension <CorrelationExtension>();

            if (extension != null)
            {
                Dictionary <string, string> dictionary = new Dictionary <string, string>();
                foreach (KeyValuePair <string, InArgument <string> > pair in this.CorrelationData)
                {
                    Fx.Assert(pair.Value != null, "pair.Value should be validated during cache metadata");
                    dictionary.Add(pair.Key, pair.Value.Get(context));
                }

                correlationHandle.InitializeBookmarkScope(context, extension.GenerateKey(dictionary));
            }
            else
            {
                throw FxTrace.Exception.AsError(new InvalidOperationException(SR2.InitializeCorrelationRequiresWorkflowServiceHost(this.DisplayName)));
            }
        }
 public CorrelationHandle GetAmbientCorrelation(NativeActivityContext context)
 {
     if (!this.triedAmbientCorrelation)
     {
         this.triedAmbientCorrelation = true;
         this.ambientCorrelation      = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
     }
     return(this.ambientCorrelation);
 }
        private static void InitializeCorrelationHandles(NativeActivityContext context, CorrelationHandle selectHandle, CorrelationHandle ambientHandle, Collection <CorrelationInitializer> additionalCorrelations, InstanceKey instanceKey, ICollection <InstanceKey> additionalKeys)
        {
            bool flag = false;

            if ((instanceKey != null) && instanceKey.IsValid)
            {
                if (selectHandle != null)
                {
                    selectHandle.InitializeBookmarkScope(context, instanceKey);
                }
                else if (ambientHandle != null)
                {
                    ambientHandle.InitializeBookmarkScope(context, instanceKey);
                    flag = true;
                }
                else if (context.DefaultBookmarkScope.IsInitialized)
                {
                    if (context.DefaultBookmarkScope.Id != instanceKey.Value)
                    {
                        throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.CorrelationHandleInUse(context.DefaultBookmarkScope.Id, instanceKey.Value)));
                    }
                }
                else
                {
                    context.DefaultBookmarkScope.Initialize(context, instanceKey.Value);
                }
            }
            if ((additionalKeys != null) && (additionalCorrelations != null))
            {
                IEnumerator <CorrelationInitializer> enumerator = additionalCorrelations.GetEnumerator();
                foreach (InstanceKey key in additionalKeys)
                {
                    while (enumerator.MoveNext())
                    {
                        QueryCorrelationInitializer current = enumerator.Current as QueryCorrelationInitializer;
                        if (current != null)
                        {
                            CorrelationHandle handle = (current.CorrelationHandle != null) ? current.CorrelationHandle.Get(context) : null;
                            if (handle == null)
                            {
                                if ((ambientHandle == null) || flag)
                                {
                                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.QueryCorrelationInitializerCannotBeInitialized));
                                }
                                handle = ambientHandle;
                                flag   = true;
                            }
                            handle.InitializeBookmarkScope(context, key);
                            break;
                        }
                    }
                }
            }
        }
Beispiel #5
0
        public static void InitializeCorrelationHandles(NativeActivityContext context,
                                                        CorrelationHandle selectHandle, CorrelationHandle ambientHandle, Collection <CorrelationInitializer> additionalCorrelations,
                                                        MessageProperties messageProperties)
        {
            CorrelationMessageProperty correlationMessageProperty;

            if (CorrelationMessageProperty.TryGet(messageProperties, out correlationMessageProperty))
            {
                InitializeCorrelationHandles(context, selectHandle, ambientHandle, additionalCorrelations,
                                             correlationMessageProperty.CorrelationKey, correlationMessageProperty.AdditionalKeys);
            }
        }
        private void ServerScheduleOnReceivedMessage(NativeActivityContext executionContext, ReceiveMessageInstanceData instance)
        {
            System.ServiceModel.Channels.Message message = instance.CorrelationResponseContext.WorkflowOperationContext.Inputs[0] as System.ServiceModel.Channels.Message;
            this.Message.Set(executionContext, message);
            instance.CorrelationResponseContext.MessageVersion = ((System.ServiceModel.Channels.Message)instance.CorrelationResponseContext.WorkflowOperationContext.Inputs[0]).Version;
            CorrelationHandle ambientCorrelation = instance.GetAmbientCorrelation(executionContext);
            CorrelationHandle selectHandle       = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(executionContext);

            MessagingActivityHelper.InitializeCorrelationHandles(executionContext, selectHandle, ambientCorrelation, this.correlationInitializers, instance.CorrelationResponseContext.WorkflowOperationContext.OperationContext.IncomingMessageProperties);
            CorrelationHandle explicitChannelCorrelation = CorrelationHandle.GetExplicitChannelCorrelation(executionContext, this.correlationInitializers);

            if (this.IsOneWay)
            {
                if (explicitChannelCorrelation != null)
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.RequestReplyHandleShouldNotBePresentForOneWay));
                }
                if (this.NoPersistHandle != null)
                {
                    System.Activities.NoPersistHandle handle4 = this.NoPersistHandle.Get(executionContext);
                    if (handle4 != null)
                    {
                        handle4.Enter(executionContext);
                    }
                }
            }
            else if (explicitChannelCorrelation != null)
            {
                if (!explicitChannelCorrelation.TryRegisterResponseContext(executionContext, instance.CorrelationResponseContext))
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.TryRegisterRequestContextFailed));
                }
            }
            else if (!ambientCorrelation.TryRegisterResponseContext(executionContext, instance.CorrelationResponseContext))
            {
                throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.ReceiveMessageNeedsToPairWithSendMessageForTwoWayContract(this.OperationName)));
            }
            if (instance.CorrelationCallbackContext != null)
            {
                CorrelationHandle explicitCallbackCorrelation = CorrelationHandle.GetExplicitCallbackCorrelation(executionContext, this.correlationInitializers);
                if (explicitCallbackCorrelation == null)
                {
                    explicitCallbackCorrelation = ambientCorrelation;
                }
                if (explicitCallbackCorrelation != null)
                {
                    explicitCallbackCorrelation.CallbackContext = instance.CorrelationCallbackContext;
                }
            }
            this.FinalizeScheduleOnReceivedMessage(executionContext, instance);
        }
Beispiel #7
0
        public static Message InitializeCorrelationHandles(NativeActivityContext context,
                                                           CorrelationHandle selectHandle, CorrelationHandle ambientHandle, Collection <CorrelationInitializer> additionalCorrelations,
                                                           CorrelationKeyCalculator keyCalculator, Message message)
        {
            InstanceKey instanceKey;
            ICollection <InstanceKey> additionalKeys;

            //
            MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);

            if (keyCalculator.CalculateKeys(buffer, message, out instanceKey, out additionalKeys))
            {
                InitializeCorrelationHandles(context, selectHandle, ambientHandle, additionalCorrelations, instanceKey, additionalKeys);
            }
            return(buffer.CreateMessage());
        }
Beispiel #8
0
 protected override void Execute(NativeActivityContext context)
 {
     if (this.Body != null)
     {
         CorrelationHandle property = null;
         if ((this.CorrelatesWith != null) && (this.CorrelatesWith.Expression != null))
         {
             property = this.CorrelatesWith.Get(context);
         }
         if (property == null)
         {
             property = this.declaredHandle.Get(context);
         }
         context.Properties.Add(CorrelationHandle.StaticExecutionPropertyName, property);
         context.ScheduleActivity(this.Body);
     }
 }
        protected override void Execute(NativeActivityContext executionContext)
        {
            CorrelationRequestContext context;
            CorrelationHandle         handle = (this.CorrelatesWith == null) ? null : this.CorrelatesWith.Get(executionContext);
            bool flag = false;
            CorrelationHandle ambientCorrelation = null;

            if (handle == null)
            {
                ambientCorrelation = executionContext.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
                flag = true;
                if (ambientCorrelation != null)
                {
                    handle = ambientCorrelation;
                }
            }
            if ((handle != null) && handle.TryAcquireRequestContext(executionContext, out context))
            {
                ReceiveMessageInstanceData instance = new ReceiveMessageInstanceData(context);
                if (flag)
                {
                    instance.SetAmbientCorrelation(ambientCorrelation);
                }
                this.ClientScheduleOnReceivedMessage(executionContext, instance);
            }
            else
            {
                if (ambientCorrelation == null)
                {
                    ambientCorrelation = executionContext.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
                }
                if ((!this.IsOneWay && (ambientCorrelation == null)) && (CorrelationHandle.GetExplicitChannelCorrelation(executionContext, this.correlationInitializers) == null))
                {
                    throw System.ServiceModel.Activities.FxTrace.Exception.AsError(new InvalidOperationException(System.ServiceModel.Activities.SR.ReceiveMessageNeedsToPairWithSendMessageForTwoWayContract(this.OperationName)));
                }
                BookmarkScope scope = (handle != null) ? handle.EnsureBookmarkScope(executionContext) : executionContext.DefaultBookmarkScope;
                if (this.onMessageBookmarkCallback == null)
                {
                    this.onMessageBookmarkCallback = new BookmarkCallback(this.OnMessage);
                }
                executionContext.CreateBookmark(this.OperationBookmarkName, this.onMessageBookmarkCallback, scope);
            }
        }
Beispiel #10
0
        internal static CorrelationHandle GetTypedCorrelationHandle(NativeActivityContext context, Collection <CorrelationInitializer> correlationInitializers, Type correlationInitializerType)
        {
            CorrelationHandle typedCorrelationHandle = null;

            if (correlationInitializers != null && correlationInitializers.Count > 0)
            {
                foreach (CorrelationInitializer correlation in correlationInitializers)
                {
                    if (correlationInitializerType == correlation.GetType())
                    {
                        typedCorrelationHandle = correlation.CorrelationHandle.Get(context);

                        // We return the first handle we find
                        break;
                    }
                }
            }

            return(typedCorrelationHandle);
        }
        private void ClientScheduleOnReceiveMessageCore(NativeActivityContext executionContext, ReceiveMessageInstanceData instance)
        {
            instance.InitializeContextAndCallbackContext();
            CorrelationHandle ambientCorrelation = instance.GetAmbientCorrelation(executionContext);

            if (instance.CorrelationRequestContext.CorrelationKeyCalculator != null)
            {
                instance.CorrelationRequestContext.Reply = MessagingActivityHelper.InitializeCorrelationHandles(executionContext, null, ambientCorrelation, this.correlationInitializers, instance.CorrelationRequestContext.CorrelationKeyCalculator, instance.CorrelationRequestContext.Reply);
            }
            if (instance.CorrelationContext != null)
            {
                CorrelationHandle explicitContextCorrelation = CorrelationHandle.GetExplicitContextCorrelation(executionContext, this.correlationInitializers);
                if (explicitContextCorrelation == null)
                {
                    explicitContextCorrelation = ambientCorrelation;
                }
                if (explicitContextCorrelation != null)
                {
                    explicitContextCorrelation.Context = instance.CorrelationContext;
                }
            }
            System.ServiceModel.Channels.Message reply = instance.CorrelationRequestContext.Reply;
            this.Message.Set(executionContext, reply);
        }
 public void SetAmbientCorrelation(CorrelationHandle ambientCorrelation)
 {
     this.ambientCorrelation      = ambientCorrelation;
     this.triedAmbientCorrelation = true;
 }
Beispiel #13
0
        // both receive and send initialize correlations using this method
        // if selectHandle is not null, we first try to initalize instanceKey with it , else we try to initalize the ambient handle
        // if ambient handle is not used for initializing instance key , we might use it for initalizing  queryCorrelationsInitalizer.

        // SelectHandle usage:
        // Receive: selectHandle is the correlatesWith handle
        // SendReply: in case of context based correlation, this is the context handle
        // Send: in case of context based correlation, this will be the callback handle
        // ReceiveReply: selectHandle will be always null
        // Note that only Receive can initialize a content based correlation with a selectHandle (parallel convoy)
        internal static void InitializeCorrelationHandles(NativeActivityContext context,
                                                          CorrelationHandle selectHandle, CorrelationHandle ambientHandle, Collection <CorrelationInitializer> additionalCorrelations,
                                                          InstanceKey instanceKey, ICollection <InstanceKey> additionalKeys)
        {
            bool isAmbientHandleUsed = false;

            if (instanceKey != null && instanceKey.IsValid)
            {
                if (selectHandle != null)
                {
                    selectHandle.InitializeBookmarkScope(context, instanceKey);
                }
                else if (ambientHandle != null)
                {
                    ambientHandle.InitializeBookmarkScope(context, instanceKey);
                    isAmbientHandleUsed = true;
                }
                else if (context.DefaultBookmarkScope.IsInitialized)
                {
                    if (context.DefaultBookmarkScope.Id != instanceKey.Value)
                    {
                        throw FxTrace.Exception.AsError(
                                  new InvalidOperationException(SR2.CorrelationHandleInUse(context.DefaultBookmarkScope.Id, instanceKey.Value)));
                    }
                }
                else
                {
                    context.DefaultBookmarkScope.Initialize(context, instanceKey.Value);
                }
            }

            if (additionalKeys != null && additionalCorrelations != null)
            {
                // The ordering of items in SelectAdditional and additional correlations are the same
                // Therefore, we assign keys iteratively

                IEnumerator <CorrelationInitializer> enumerator = additionalCorrelations.GetEnumerator();

                foreach (InstanceKey key in additionalKeys)
                {
                    Fx.Assert(key != null && key.IsValid, "only valid keys should be passed into InitializeCorrelationHandles");

                    while (enumerator.MoveNext())
                    {
                        QueryCorrelationInitializer queryCorrelation = enumerator.Current as QueryCorrelationInitializer;
                        if (queryCorrelation != null)
                        {
                            CorrelationHandle handle = (queryCorrelation.CorrelationHandle != null ? queryCorrelation.CorrelationHandle.Get(context) : null);
                            if (handle == null)
                            {
                                if (ambientHandle != null && !isAmbientHandleUsed)
                                {
                                    handle = ambientHandle;
                                    isAmbientHandleUsed = true;
                                }
                                else
                                {
                                    throw FxTrace.Exception.AsError(
                                              new InvalidOperationException(SR2.QueryCorrelationInitializerCannotBeInitialized));
                                }
                            }
                            handle.InitializeBookmarkScope(context, key);
                            break;
                        }
                    }
                }
            }
        }
 public static bool TryGetCallbackCorrelationHandle(this Collection<CorrelationInitializer> correlationInitializers, NativeActivityContext context, out CorrelationHandle correlationHandle)
 {
     correlationHandle = CorrelationHandle.GetExplicitCallbackCorrelation(context, correlationInitializers);
     return correlationHandle != null;
 }
 void UpdateE2ETracingId(Guid e2eTracingId, CorrelationHandle correlatesWith, CorrelationHandle ambientHandle, CorrelationHandle requestReplyHandle)
 {
     if (correlatesWith != null)
     {
         correlatesWith.E2ETraceId = e2eTracingId;
     }
     else if (ambientHandle != null)
     {
         ambientHandle.E2ETraceId = e2eTracingId;
     }
     else if (requestReplyHandle != null)
     {
         requestReplyHandle.E2ETraceId = e2eTracingId;
     }
 }
 public void SetAmbientCorrelation(CorrelationHandle ambientCorrelation)
 {
     Fx.Assert(!this.triedAmbientCorrelation, "can only set ambient correlation once");
     this.ambientCorrelation = ambientCorrelation;
     this.triedAmbientCorrelation = true;
 }
            public CorrelationHandle GetAmbientCorrelation(NativeActivityContext context)
            {
                if (this.triedAmbientCorrelation)
                {
                    return this.ambientCorrelation;
                }

                this.triedAmbientCorrelation = true;
                this.ambientCorrelation = context.Properties.Find(CorrelationHandle.StaticExecutionPropertyName) as CorrelationHandle;
                return this.ambientCorrelation;
            }
        public static Message InitializeCorrelationHandles(NativeActivityContext context, CorrelationHandle selectHandle, CorrelationHandle ambientHandle, Collection <CorrelationInitializer> additionalCorrelations, CorrelationKeyCalculator keyCalculator, Message message)
        {
            InstanceKey key;
            ICollection <InstanceKey> is2;
            MessageBuffer             buffer = message.CreateBufferedCopy(0x7fffffff);

            if (keyCalculator.CalculateKeys(buffer, message, out key, out is2))
            {
                InitializeCorrelationHandles(context, selectHandle, ambientHandle, additionalCorrelations, key, is2);
            }
            return(buffer.CreateMessage());
        }
        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);
            }
        }
Beispiel #20
0
 public static bool TryGetCallbackCorrelationHandle(this Collection <CorrelationInitializer> correlationInitializers, NativeActivityContext context, out CorrelationHandle correlationHandle)
 {
     correlationHandle = CorrelationHandle.GetExplicitCallbackCorrelation(context, correlationInitializers);
     return(correlationHandle != null);
 }
Beispiel #21
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);
            }
        }
        bool TryGetCorrelatesWithHandle(NativeActivityContext context, out CorrelationHandle correlationHandle)
        {
            correlationHandle = null;
            if (this.CorrelatesWith != null)
            {
                correlationHandle = this.CorrelatesWith.Get(context);
            }

            return correlationHandle != null;
        }