小编mdo*_*tas的帖子

在WCF服务中使用async/await时,在第一次等待之后,OperationContext.Current为null

我在.NET 4.5中使用async/await模式在WCF中实现一些服务方法.示例服务:

合同:

[ServiceContract(Namespace = "http://async.test/")]
public interface IAsyncTest
{
    Task DoSomethingAsync();
}
Run Code Online (Sandbox Code Playgroud)

执行:

MyAsyncService : IAsyncTest
{
    public async Task DoSomethingAsync()
    {
        var context = OperationContext.Current; // context is present

        await Task.Delay(10);

        context = OperationContext.Current; // context is null
    }
}
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是,在第一次await OperationContext.Current返回后null我无法访问OperationContext.Current.IncomingMessageHeaders.

在这个简单的例子中,这不是问题,因为我可以在之前捕获上下文await.但是在现实世界OperationContext.Current中,正在从调用堆栈内部访问,我真的不想更改大量代码只是为了进一步传递上下文.

有没有办法在await点之后获取操作上下文而不是手动将其传递给堆栈?

.net wcf async-await .net-4.5

35
推荐指数
3
解决办法
1万
查看次数

标签 统计

.net ×1

.net-4.5 ×1

async-await ×1

wcf ×1