TransactionScopeAsyncFlowOption 不会从 WCF 客户端流向服务

TransactionScopeAsyncFlowOption does not flow from WCF client to service

我的 WCF 服务方法需要在从客户端流向服务的事务中执行并发任务。为了使事务范围能够流过线程,我在发送对服务的调用之前在事务范围class 的构造函数中启用了 TransactionScopeAsyncFlowOption。

using (var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
    //service call here. Async flow option has no effect on service side.
}

事务从客户端流向服务,但无法流向子任务。但是,如果我在服务中创建新的事务范围并在那里启用它的异步流,那么事务就会流向子任务。所以我的问题是,为什么 TransactionScopeAsyncFlow 选项对服务端的事务没有影响?它不应该采用客户端事务范围设置,这样就不需要在服务中创建新的事务范围来启用其异步流吗?

我找到了类似问题 as well. What actually happens is that WCF loses nearly all of its context upon crossing the threshold of first await and we should explicitly opt-in to flow any ambient context such as ambient transaction to subsequent async calls. There are some workarounds mentioned in this 的书。如果您需要更多详细信息,请查看它们。我将在服务操作中的异步调用之前创建新范围。