Request-Reply Correlation
This topic applies to Windows Workflow Foundation 4 (WF4).
Request-reply correlation is used with a Receive/SendReply pair to implement a two-way operation in a workflow service and with a Send/ReceiveReply pair that invokes a two-way operation in another Web service. When invoking a two-way operation in a WCF service, the service can be either a traditional imperative code-based Windows Communication Foundation (WCF) service or it can be a workflow service. To use request-reply correlation a two-way binding must be used, such as BasicHttpBinding. Whether invoking or implementing a two-way operation, the correlation initialization steps are similar and are covered in this section.
Using Correlation in a Two-Way Operation with Receive/SendReply
A Receive/SendReply pair is used to implement a two-way operation in a workflow service. The runtime uses request-reply correlation to ensure that the reply is dispatched to the correct caller. When a workflow is hosted using WorkflowServiceHost, which is the case for workflow services, then the default correlation initialization is sufficient. In this scenario, a Receive/SendReply pair is used by a workflow, and no specific correlation configuration is required.
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder"
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
// Construct a workflow using StartOrder and ReplyToStartOrder.
Explicitly Initializing Request-Reply Correlation
If other two-way operations are in parallel, then correlation should be explicitly configured. This can be done by specifying a CorrelationHandle and RequestReplyCorrelationInitializer, or by placing the Receive/SendReply inside of a CorrelationScope. In this example, request-reply correlation is configured on a Receive/SendReply pair.
Variable<CorrelationHandle> RRHandle = new Variable<CorrelationHandle>();
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder",
CorrelationInitializers =
{
new RequestReplyCorrelationInitializer
{
CorrelationHandle = RRHandle
}
}
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
// Construct a workflow using StartOrder and ReplyToStartOrder.
Instead of explicitly configuring the correlation, a CorrelationScope activity can be used. CorrelationScope provides an implicit CorrelationHandle to the messaging activities that it contains. In this example, a Receive/SendReply pair is contained inside a CorrelationScope. No explicit correlation configuration is required.
Receive StartOrder = new Receive
{
CanCreateInstance = true,
ServiceContractName = OrderContractName,
OperationName = "StartOrder"
};
SendReply ReplyToStartOrder = new SendReply
{
Request = StartOrder,
Content = … // Contains the return value, if any.
};
CorrelationScope s = new CorrelationScope
{
Body = new Sequence
{
Activities =
{
StartOrder,
// Activities that create the reply.
ReplyToStartOrder
}
}
};
// Construct a workflow using the CorrelationScope.
If additional correlations are required then they can be configured using the CorrelationInitializers property of the respective messaging activities using the desired CorrelationInitializer types.
Using Correlation in a Two-Way Operation with Send/ReceiveReply
While the Receive activity can only be used in a workflow service hosted by WorkflowServiceHost, Send and the Send/ReceiveReply pair can be used in any workflow that must invoke a method on a Web service. If the workflow is hosted using WorkflowServiceHost then the default correlation described in the previous section applies, but if not, then correlation must be configured either explicitly using the desired CorrelationInitializer and CorrelationHandle, or by using the implicit handle management of the CorrelationScope.
When using Add Service Reference on a service with two-way operations, activities are generated that wrap a Send/ReceiveReply pair activity internally with the Request/Reply correlation explicitly specified.