6

WCF で C#5 Async-Await を使用すると、残りのコードが別のスレッドで続行される場合に待機した後、現在の操作コンテキストが失われます。(OperationContext.Current は null です)。

別の外部サービスを呼び出す WCF サービスに取り組んでいます。また、操作コンテキストにアクセスする外部サービス呼び出しで使用されるカスタム バインディング拡張機能がいくつかあります。したがって、この呼び出し中にコンテキストを伝播する必要があり、操作コンテキストをローカル変数にコピーするだけでは機能しません。

私の設定は次のようになります

<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」は、操作コンテキストにアクセスして情報を取得します。

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

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

    return response.text;
}

OperationContext を外部サービス呼び出しに伝達し、その後、残りのコード実行に伝達する良い方法はありますか?

4

1 に答える 1

4

カスタム同期コンテキストを使用できます。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);
}
于 2014-03-21T09:17:26.410 に答える