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

前端 未结 7 583
礼貌的吻别
礼貌的吻别 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:27

    Fortunately for us, our real-life service implementation gets instantiated via Unity IoC container. That allowed us to create a IWcfOperationContext which was configured to have a PerResolveLifetimeManager which simply means that there will be only one instance of WcfOperationContext for each instance of our RealService.
    In the constructor of WcfOperationContext we capture OperationContext.Current and then all the places that require it get it from IWcfOperationContext. This is in effect what Stephen Cleary suggested in his answer.

    0 讨论(0)
提交回复
热议问题