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

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

    Update: As pointed out by in the comments below, this solution is not thread safe, so I guess the solutions discussed above is still the best way.

    I get around with the problem by registering the HttpContext into my DI container (Application_BeginRequest) and resolve it whenever I need it.

    Register:

    this.UnityContainer.RegisterInstance(new HttpContextWrapper(HttpContext.Current));
    

    Resolve:

    var context = Dependencies.ResolveInstance();
    

提交回复
热议问题