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:
How can we receive the messages with the same instance? Is easy making a little trick in the receive shape:
- We create a correlation handler. Let’s call it singleIntanceHandler
- In the receive shape set the CorrelatesWith property with the singleInstanceHandler.
- In the CorrelatesOn definition you should insert a string, not an xpath expression. For instance: “asdf”.
- 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: