More on using a TransactionScopeActivity within a ReceiveActivity

Part 1
part 2
Part 3
Part 4

In a previous blog post I write about what happens when you place a TransactionScopeActivity within a ReceiveActivity and an exception occurs that is supposed to roll back the transaction. In short the story was very bad and we could come up with only a partial workaround, not a pretty sight.


But there is more to it than just that little horror story. Suppose you do the obvious and place the a TransactionScopeActivity within a ReceiveActivity and no exception occurs. Say like the workflow below, please note that the codeActivity1 only sets the return value and causes no error.




Now the transaction is committed and the WCF call returns perfectly normally so everything is good right. Well not quite [:(]

The obvious first point is that the TransactionScopeActivity serves no real purpose. After all if it isn’t allowed to fail under any circumstances why bother with it in the first place. Well ok there is the point of doing several updates as a single transaction so other users cannot see a partially committed order but that is about it.

But that is actually the least of my worries as there is a far bigger issue to worry about and that is called workflow persistence.

Yes that is right. After all when we are using a TransactionScopeActivity workflow persistence is mandatory. The TransactionScopeActivity is decorated with the PersistOnClose attribute so the state of the workflow will be persisted as soon as the transaction is complete. And normally that is a good thing but in this case it is the cause of the second problem because this is still inside the ReceiveActivity. So basically we are storing the workflow state as it is just before returning an answer to the client. Now if everything is running fine that won’t matter because the workflow will continue until it is finished and everyone is happy. But suppose the workflow host terminates before the workflow is finished? In this test I added the DelayActivity, and set UnloadOnIdle to false so it doesn’t persist the state, giving me the opportunity to kill the workflow runtime. Now if I restart the runtime It is going to reload the last state of the workflow and continue from there. And guess what it’s going to do first? You guessed it: it is going to send the response to the client for a second time. Of course the client is no longer around and that action fails with an InvalidOperationException with message “Workflow unloaded between request & response.”

I guess the message is not entirely correct as it should say “Workflow reloaded between request & response.” but it’s close enough.

The bottom line is the workflow terminates unless you specifically allow for this to happen and catch the error.


So basically we have a choice between putting the TransactionScopeActivity inside a ReceiveActivity, not being able to throw an error and having a restore problem, or putting the TransactionScopeActivity around the ReceiveActivity, something that only works with a sequential workflow and an initiating ReceiveActivity as I described in the previous post.

I guess these options make me pretty unhappy [:(].


Enjoy your workflow transactions!

2 thoughts on “More on using a TransactionScopeActivity within a ReceiveActivity

  1. Unfortunately this is not supported in 3.5 and you hit numerous issues if you try (as you’ve experienced). RecieveMessage in 3.5 was authored as a non-persistable activity (the data it receives is not necessarily persistence-friendly), so either going idle or hitting a persist point (through transactions or [PersistOnClose]) will cause ReceiveMessage all sorts of problems. Life gets better in 4.0 and I’ll be able to provide those details after PDC (late October).

  2. FWIW, the documentation also mentions this (though it isn’t particularly helpful since you don’t get viable alternatives in 3.5)

    “To ensure that persistence performs properly and does not persist transient messages, make sure that child activities of the ReceiveActivity do not persist by themselves. This can occur if the child activities go idle when a persistence provider was created with UnloadOnIdle set to true, for instance. ”

    It is an unfortunate limitation, and one we plan on addressing in the next release.

Leave a Reply

Your email address will not be published. Required fields are marked *