Monday, January 11, 2016

Using a TransactionScope with async/await

Maybe you didn’t know but the System.Transactions.TransactionScope object in .NET doesn’t play nice together with the async/await syntax. As long as all the code inside the TransactionScope is executed on the same thread, there is no issue. But when you start combining it with async/await you get into trouble. The following code doesn’t work in .NET 4.5:

Whenyou try to execute this code, it will fail with the following error:

System.InvalidOperationException : A TransactionScope must be disposed on the same thread that it was created.

image

A fix is available in .NET 4.5.1 but requires some code changes. There is a new enumeration TransactionScopeAsyncFlowOption that can be provided in the TransactionScope constructor. Here is the fixed code:

No comments: