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

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

    It is unfortunate that this doesn't work and we will see about getting a fix out in a future release.

    In the mean time, there is a way to reapply the context to the current thread so that you don't have to pass the object around:

        public async Task Add(double n1, double n2)
        {
    
            OperationContext ctx = OperationContext.Current;
    
            await Task.Delay(100);
    
            using (new OperationContextScope(ctx))
            {
                DoSomethingElse();
            }
            return n1 + n2;
        }  
    

    In the above example, the DoSomethingElse() method will have access to OperationContext.Current as expected.

提交回复
热议问题