How to dispose TransactionScope in cancelable async/await?

前端 未结 5 1749

I\'m trying to use the new async/await feature to asynchronously work with a DB. As some of the requests can be lengthy, I want to be able to cancel them. The issue I\'m run

5条回答
  •  天涯浪人
    2020-12-24 01:05

    I know this is an old thread, but if anyone has run into the problem System.InvalidOperationException : A TransactionScope must be disposed on the same thread that it was created.

    The solution is to upgrade to .net 4.5.1 at a minimum and use a transaction like the following:

    using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
    {
       //Run some code here, like calling an async method
       await someAsnycMethod();
       transaction.Complete();
    } 
    

    Now the transaction is shared between methods. Take a look at the link below. It provide a simple example and more detail

    For complete details, take a look at This

提交回复
热议问题