Using the sequential workflow built in the previous post, we will continue the correlation example using Windows Workflow 4.0.
The following illustrates the current workflow so far:

The concept is the workflow instance will now wait until either an OrderApproval for the same order is received or a specified amount of time elapses. This is done by using a Pick shape with two branches. In the screenshot below, note the altered display name properties:

To capture the OrderApproval message, drop a ReceiveAndSendReply shape into the Trigger section of the ApprovalReceived shape. Set the receive shape’s DisplayName and OperationName to ReceiveApproval and SubmitApproval. Set the DisplayName of the SendReply shape to AcknowledgeApproval:

Next specify the approval message to be received. This will contain two parameters: the orderNumber to be used to correlate and an OrderApproval object. Note: specify the Assign To value for the approval parameter.

The reply will contain a string to acknowledge the approval was received. This is specified in the SendReply shape’s Content property:
System.String.Format("Order {0} approval was set to {1} on {2}.", order.OrderNumber, order.Approval.WasApproved, order.Approval.DateApproved)

In the ApprovalNotReceived branch, drop a Delay shape in the Trigger section and a TerminateWorkflow shape in the Action section:

Change the Delay shape’s properties as follows (for this example, 30 seconds will be the duration to wait until an approval is received):

The TerminateWorkflow shape will need an Exception specified and a Reason entered:
System.String.Format("Order {0} was not approved within the Timeout period.", order.OrderNumber)

Now to correlate the two messages so the OrderApproval is associated with the initial Order.
First define a new CorrelationHandle variable with the scope of the entire Sequential Service:

In the ReceiveNewOrder shape, set the CorrelatesWith property to the corr variable:

Initialize the CorrelationHandle by adding the variable in the CorrelationInitializers property:

In the XPath Queries section specify to use the value of the Order.OrderNumber received in the message:

To associate the OrderApproval message, set the ReceiveApproval’s CorrelatesWith property to use the corr variable:

Instead of initializing a new CorrelationHandle, we are going to use the existing handle so set the CorrelatesOn property (note the Query specifies the orderNumber parameter):

That’s it. So run the project to verify the correlation works.
Notice the project now has two endpoints: one for submitting the initial order and one for submitting the approval:

Invoke a 101 order and verify the result:

Within 30 seconds submit an approval for order 101 (in my example, I did not approve the order):

To test the timeout, submit another order and wait 30 seconds. Now check the Tracked Events in the AppFabric dashboard (it might take a little while before the event is added to monitoring):

In the screenshot above, note the Reason is specified in the Errors tab.
Posted
Fri, Jan 14 2011 8:24 AM
by
chilberto