WCF Async-Await does not support OperationContext RRS feed

  • Question

  • When using C#5 Async-Await in WCF, after an await if rest of the code continues on a different thread, we loose the Current Operation Context. (OperationContext.Current is null). It does not flow between threads like the HttpContext  in ASP.Net with AsyncAwait.

    Is this going to be fixed soon in any upcoming Releases?

    Monday, December 30, 2013 3:42 AM

All replies

  • Hi,

    I think that we will see above getting a fix out in a future release. 

    Please try to check this tread: .

    Actually inside the task body the property OperationContext.Current is null. But we can initialize it outside the task but use it inside the task.      

     public string GetData(int value)
                 OperationContext oc = OperationContext.Current;
                Task<int> t = Task<int>.Factory.StartNew(() =>
                     return oc.Host.BaseAddresses.Count;
                return string.Format("You entered: {0}", t.Result);

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 31, 2013 2:20 AM
  • Thanks for the reply Amy. Actually my problem is a bit more complicated

    I have a WCF Service which calls another external Service. And there are a few Custom Binding Extensions used in the external service call which access the Operation Context. So I need the Context to be propagated during this call.     

    	<binding name="MyCustomBinding">
              <security defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport" />
              <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16" messageVersion="Soap12" writeEncoding="utf-8">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
              <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"  maxReceivedMessageSize="65536" />
          <endpoint address="https://ExternalService.svc/soap12" binding="customBinding" bindingConfiguration="MyCustomBinding" contract="Contract" name="ExternalService"/>
            <add name="MyBindingExtention" type="Bindings.MyBindingExtention, Services, Version=, Culture=neutral, PublicKeyToken=null"/>
      "MyBindingExtention" accesses the operation context to get some information. 
     public async Task<string> GetData(int value)
                 OperationContext oc = OperationContext.Current;
                //External Web service Call
    		     var response = await externalService.GetDataAsync();
                return response.Information;

    Is there a good way to make this work? I really want to avoid writing a custom SynchronisationContext

    Thursday, January 2, 2014 2:38 AM