single instance of a workflow in the cluster with WF 4.0 and AppFabric


In my current project we need to have just one instance of a wf service in the cluster. That instance should be automatically created, that’s easy calling the receive in the global.asax, here is a good example:

But what happens if we have several machines trying to initialize the workflow? We need to create one, and just one, instance in a receive but later that instance should be the one who accepts the following messages, avoiding creating other instances.

The overall picture is the following parallel, in a branch we will be always receiving the initialization calls and in the other we will be executing the logic:

Single instance wf implementation

How can we receive the messages with the same instance? Is easy making a little trick in the receive shape:

  1. We create a correlation handler. Let’s call it singleIntanceHandler
  2. In the receive shape set the CorrelatesWith property with the singleInstanceHandler.
  3. In the CorrelatesOn definition you should insert a string, not an xpath expression. For instance: “asdf”.
  4. Leave the correlation initializer just with the handle for the response.

As I understand the first time it creates the instance initializating the correlation with “asdf”, the next time it receives a message, AppFabric looks if there is an instance that he could correlate with the string “asdf”, and it finds the one initialize in the first call.

Here is the code:!Av_YzZiT16fZ1gWUs1-HPuG5qIoZ



  1. […] All of this fits perfectly with the single instance pattern that I exposed in my last post. […]

  2. Denis Robertson · · Reply

    This is a great post it raises some very interesting possibilities. Link to the example code appears to be dead would it be possible to re-post it?

    1. I think now the link must work. Anyhow appfabric has been discontinued and we didn’t have a really really good experience with it. I would switch to nservicebus or something similar.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: