OperationContext.Current is null after first await when using async/await in WCF service

前端 未结 7 605
礼貌的吻别
礼貌的吻别 2020-12-02 22:59

I am using async/await pattern in .NET 4.5 to implement some service methods in WCF. Example service:

Contract:

[ServiceContract(Namespace = \"http:/         


        
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 23:02

    I think your best option is to actually capture it and pass it manually. You may find this improves the testability of your code.

    That said, there are a couple of other options:

    1. Add it to the LogicalCallContext.
    2. Install your own SynchronizationContext which will set OperationContext.Current when it does a Post; this is how ASP.NET preserves its HttpContext.Current.
    3. Install your own TaskScheduler which sets OperationContext.Current.

    You may also want to raise this issue on Microsoft Connect.

提交回复
热议问题