How to support async methods in a TransactionScope with Microsoft.Bcl.Async in .NET 4.0?

二次信任 提交于 2019-12-03 05:36:12
Henrik Hjalmarsson

You have to use TransactionScopeAsyncFlowOption.Enabled

public static TransactionScope CreateAsyncTransactionScope(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
    {
        var transactionOptions = new TransactionOptions
        {
            IsolationLevel = isolationLevel,
            Timeout = TransactionManager.MaximumTimeout
        };
        return new TransactionScope(TransactionScopeOption.Required, transactionOptions, TransactionScopeAsyncFlowOption.Enabled);
    }

Not sure if this fits your scenario but ConfigureAwait(false) can be used in an ASP.NET app to make sure an awaited function call re-enters the calling request context.

So if this code is running in an ASP.NET app the following code:

await _repository.SaveItemAsync(item).ConfigureAwait(false);

Would ensure that execution would continue on the request thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!