The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog





  • View Maurice De Beijer's profile on LinkedIn


Email Notifications



Activity correlation in Windows Workflow Foundation 4

Note: This blog post is written using the .NET framework 4.0 Beta 2

There are two types of correlation to think about in Windows Workflow Foundation:

  • Message correlation
    Basically sending multiple requests to the same workflow.
  • Activity correlation
    Making sure two activities work together.

In this post I am going to show activity correlation


Activity correlation is used when multiple activities form a single logical action. Think about the Send and ReceiveReply activities. Two distinct activities but they work together because the first, the Send activity, send a WCF request somewhere and the second, the ReceiveReply activity, receives the response. The same goes when receiving messages.

The good thing is that when we use the ReceiveAndSendReply or SendAndReceiveReply templates everything will be done for us and the required activity correlation will be configured automatically. However sometimes we might need to do so ourselves because we create a workflow in a different way, like using regular code.


So when we start using the Send and ReceiveReply or the Receive and SendReply activities from code we need to do a bit more work. How much varies and depends on both the workflow and the hosting environment we use. The big exception to the rule is when we use the WorkflowServiceHost and no parallel or overlapping messaging activities. In that case life is simple and the WorkflowServiceHost takes care of all required correlation for us. Nice Smile

That does leave for quite a few scenarios where we do need to add activity correlation though. The obvious one is using the WorkflowApplication or WorkflowInvoker to run our workflows. Fortunately this produces an System.InvalidOperationException with a message like:

The Send activity is configured with a request/reply Operation 'GetData', however, there is no ReceiveReply activity paired with it. Please pair Send with ReceiveReply and correlate them using a CorrelationHandle.

There are different ways to configure activity correlation but the easiest way is to wrap the activity pair in a CorrelationScope activity. The CorrelationScope will automatically create the required correlation handle and the messaging activities will use this handle.


Show me the code

The code below calls a very standard and basic WCF service passing in the value if 42 and prints the result. The important part is that the send are receive actions are done inside of a CorrelationScope activity so this workflow can run in any host.

static Activity CreateWorkflow()
    var getDataResult = new Variable<string>();
    var send = new Send()
        OperationName = "GetData",
        ServiceContractName = "IService1",
        Endpoint = new Endpoint()
            Binding = new BasicHttpBinding(),
            AddressUri = new Uri("http://localhost:8080/GetDataService/")
        Content = new SendParametersContent()
            Parameters = 
                {"value", new InArgument<int>(42)}
    var receive = new ReceiveReply()
        Request = send,
        Content = new ReceiveParametersContent
            Parameters =
                {"GetDataResult", new OutArgument<string>(getDataResult)}
    var workflow = new Sequence()
         Variables = { getDataResult },
         Activities = 
            new CorrelationScope() 
                Body = new Sequence() 
                    Activities = { send, receive } 
            new WriteLine() { Text = getDataResult } }
    return workflow;


Update: See here for the code when using a RequestReplyCorrelationInitializer.



Published Wed, Dec 2 2009 18:57 by Maurice
Filed under: , , , ,


# Activity correlation using a RequestReplyCorrelationInitializer@ Thursday, December 03, 2009 12:17 PM

  In my previous post about Windows Workflow Foundation 4 I used the CorrelationScope activity to

# Mr Re-start on Final State when using onWorkflowItemChanged()@ Monday, January 11, 2010 2:14 AM

I am currently working on custom State machine workflow using VS 2008. My Workflow has Initialization State, First State, Second State and Final State. I am using onWorkflowItemChanged Event in First State and Second State and simply checking the Status of Item. The Status of item can be "FirstState","SecondState","Completed". I am simply checking if item status updated to Completed, move workflow to Final State (end workflow ).

Now my problem is that when workflow moves to final State, rather then workflow should stop it goes re-activate (workflow moves back to initail state) and history log associated to workflow becomes clear.

Every thing is fine in my worflow but i just want to stop worflow properly.

Kindly help me. I tried my best to solve this issue but could not found any clue.

Thanks and advance.


by Adnan