将OperationContext传播到异步WCF调用

时间:2014-01-02 05:44:06

标签: c# wcf async-await c#-5.0 operationcontext

在WCF中使用C#5 Async-Await,在await之后,如果其余代码继续在另一个线程上,我们将松开当前操作上下文。 (OperationContext.Current为null)。

我正在开发一个调用另一个外部服务的WCF服务。并且在外部服务调用中使用了一些自定义绑定扩展来访问操作上下文。所以我需要在调用期间传播Context,它不能将操作上下文复制到局部变量中。

我的配置看起来像这样

<system.serviceModel>
<bindings>
  <customBinding>
<binding name="MyCustomBinding">
      <MyBindingExtention/>
      <security authenticationMode="UserNameOverTransport" />
      <textMessageEncoding maxReadPoolSize="64" >
        <readerQuotas maxStringContentLength="8192" />
      </textMessageEncoding>
      <httpsTransport manualAddressing="false" maxReceivedMessageSize="65536" />
    </binding>
 </customBinding>
 <client>
  <endpoint address="https://ExternalService.svc" 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”访问 operationcontext 以获取一些信息。

public async Task<string> GetExternalData(int value)
{
    var oc = OperationContext.Current;

    //External Web service Call
    var response = await externalService.GetDataAsync();

    return response.text;
}

是否有一种很好的方法可以将OperationContext传播到外部服务调用中,然后再次传入剩余的代码执行中?

1 个答案:

答案 0 :(得分:3)

您可以使用自定义同步上下文。以下是SynchronizationContext实施示例:

public class OperationContextSynchronizationContext : SynchronizationContext
{
    private readonly OperationContext context;

    public OperationContextSynchronizationContext(IClientChannel channel) : this(new OperationContext(channel)) { }

    public OperationContextSynchronizationContext(OperationContext context)
    {
        OperationContext.Current = context;
        this.context = context;
    }

    public override void Post(SendOrPostCallback d, object state)
    {
        OperationContext.Current = context;
        d(state);
    }
}

用法:

var currentSynchronizationContext = SynchronizationContext.Current;
try
{
    SynchronizationContext.SetSynchronizationContext(new OperationContextSynchronizationContext(client.InnerChannel));
    var response = await client.RequestAsync();
    // safe to use OperationContext.Current here
}
finally
{
    SynchronizationContext.SetSynchronizationContext(currentSynchronizationContext);
}