none
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:
    http://stackoverflow.com/questions/12797091/operationcontext-current-is-null-after-first-await-when-using-async-await-in-wcf .

    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
    Moderator
  • 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.     

     <system.serviceModel>
        <bindings>
          <customBinding>
    	<binding name="MyCustomBinding">
              <MyBindingExtention/>
              <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"/>
              </textMessageEncoding>
              <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"  maxReceivedMessageSize="65536" />
            </binding>
         </customBinding>
         <client>
          <endpoint address="https://ExternalService.svc/soap12" binding="customBinding" bindingConfiguration="MyCustomBinding" contract="Contract" name="ExternalService"/>
         </client>
      </bindings>
      <extensions>
         <bindingElementExtensions>
            <add name="MyBindingExtention" type="Bindings.MyBindingExtention, Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
          </bindingElementExtensions>
       </extensions>   
     </system.serviceModel>
       
      "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