Compartilhar via


Context Exchange Correlation

This topic applies to Windows Workflow Foundation 4 (WF4).

Context correlation is based on the context exchange mechanism described in the .NET Context Exchange Protocol Specification. Context correlation uses a well-known context header or cookie to relate messages to the correct instance. To use context correlation, a context-based binding such as BasicHttpContextBinding, WSHttpContextBinding, or NetTcpContextBinding must be used on the endpoint provided to the WorkflowServiceHost. Context correlation is different than WCF duplex binding because duplex binding requires the channel to be kept open and does not work in long-running scenarios where the channel is not kept open. For more information about duplex binding, see Duplex Services and the Duplex sample. This topic explains how to use context correlation with messaging activities in a workflow service.

Using Context Correlation

Context correlation is used when a client must make repeated calls into a workflow service and the service is hosted using one of the context bindings. This type of correlation is initialized by a Receive/SendReply pair in the workflow service. The context is sent back to the client together with the reply, and then the client attaches this context on subsequent calls to the service.

Configuring Context Correlation in a Workflow Service

Context correlation is initialized by using a ContextCorrelationInitializer that is associated with the SendReply activity that replies to the initial incoming message. In the following example, the SendReply is configured to initialize a context correlation.

Variable<string> Item = new Variable<string>();
Variable<CorrelationHandle> OrderHandle = new Variable<CorrelationHandle>();

Receive StartOrder = new Receive
{
    CanCreateInstance = true,
    ServiceContractName = "IOrderService",
    OperationName = "StartOrder"
};

SendReply ReplyToStartOrder = new SendReply
{
    Request = StartOrder,
    CorrelationInitializers =
    {
        new ContextCorrelationInitializer
        {
            CorrelationHandle = OrderHandle
        }
    }
};

Note

In this example, there are actually two types of correlation being used: context correlation and request-reply correlation. The context correlation is used so that calls from clients are routed to the correct instance. The request-reply correlation is used by the Receive and the SendReply activities together to implement the two-way operation modeled by these activities. In this example, only the context correlation is explicitly configured, and the Receive/SendReply pair is using the default request-reply correlation that is provided by the implicit CorrelationHandle management of WorkflowServiceHost. When using the ReceiveAndSendReply activity template in the workflow designer, request-reply correlation is explicitly configured. For more information aboutrequest-reply correlation and implicit correlation handle management, see Request-Reply Correlation and Correlation Overview. For more information aboutthe ReceiveAndSendReply activity template, see ReceiveAndSendReply Template Designer.

Subsequent Receive activities in the workflow service can reference the CorrelationHandle that was initialized by the SendReply in the previous example.

Receive AddItem = new Receive
{
    ServiceContractName = "IOrderService",
    OperationName = "AddItem",
    CorrelatesWith = OrderHandle
};

The endpoint is then configured on the WorkflowServiceHost to use a context-based binding, such as BasicHttpContextBinding.

<endpoint
  contract=”IOrderContract”
  binding = “basicHttpContextBinding”
  address=”https://localhost:8080/OrderService” />

Configuring Context Correlation in a Workflow Client

When the client is another workflow, context correlation must be configured on the client as well. On the client workflow, the ReceiveReply of the Send/ReceiveReply pair that makes the initial call to the workflow service must be configured with a ContextCorrelationInitializer.

Variable<CorrelationHandle> cchandle = new Variable<CorrelationHandle>();
Send request = new Send
{
    // Activity configuration omitted.
};

ReceiveReply reply = new ReceiveReply
{
    Request = request,
    CorrelationInitializers = 
    {
        new ContextCorrelationInitializer
        {
            CorrelationHandle = cchandle
        }
    }
};

After the correlation is initialized, subsequent Send activities can use the CorrelationHandle to invoke methods on the same service instance.

Send request2 = new Send
{
    CorrelatesWith = cchandle,
    // Remaining activity configuration omitted.
};

Note that in these examples, the context correlation has been explicitly configured. If the client workflow is not also hosted in a WorkflowServiceHost, then correlation must be explicitly configured, unless the activities are contained within a CorrelationScope activity. For more information about context correlation, see the NetContextExchangeCorrelation sample.

If the client that is making calls to the workflow service is not a workflow, then it can still make repeated calls as long as it explicitly passes back the context that is returned from the first call to the workflow service. Proxies generated by adding a service reference in Visual Studio 2010 store and pass this context by default.