In .NET Framework 4.5, Microsoft introduced async and await keywords in C# to simplify non-blocking programming. Microsoft has a very nice article about asynchronous programming on MSDN. However, transactions were not aware of async/await in .NET 4.5 (perhaps that would also be the reason EntityFramework did not come out with async support that time.)
In .NET Framework 4.5.1, Microsoft addressed the async issue. TransactionScope is now aware of async/await and it has now an option to enable an ambient transaction to “flow across thread continuations.” The usage is very simple:
1 2 3 4 5 6 7 8 |
using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // do stuff // await repository.DeleteAsync(...) // await context.SaveChangesAsync() // etc. transaction.Complete(); } |