1

サードパーティのRESTfullWebサービスが5秒以内に応答を返す必要があるというSLAがあります。それ以外の場合は、サービスコールを中止し、ビジネスロジックの他の部分を実行する必要があります。

誰かがC#.Netの使用についてどのように達成できるかについて私を助けてくれませんか。

4

2 に答える 2

1

WCF を使用して外部 Web サービスを呼び出す場合は、クライアント エンドポイントのバインド構成で sendTimeout 値を 5 秒に構成するだけです。次に、クライアント プロキシ オブジェクトが外部サービスから応答を取得しない場合、 TIMEoutException がスローされ、それを処理して続行できます。バインディング構成の例を以下に示します。

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="myExternalBindingConfig"
               openTimeout="00:01:00"
               closeTimeout="00:01:00"
               sendTimeout="00:05:00"
               receiveTimeout="00:01:00">
       </binding>
    </basicHttpBinding>
  </bindings>
</system.serviceModel>
于 2012-09-01T23:41:38.163 に答える
0

アンマネージ コードを扱う際に問題が発生する可能性があるため、ここで提供するプラクティスの一部は悪いと見なされるという「標準的な免責事項」を前もって提供することで、回答の前置きをさせてください。そうは言っても、ここに答えがあります:

void InitializeWebServiceCall(){

    Thread webServiceCallThread = new Thread(ExecuteWebService);
    webServiceCallThread.Start();
    Thread.Sleep(5000); // make the current thread wait 5 seconds
    if (webServiceCallThread.IsAlive()){
      webServiceCallThread.Abort(); // warning for deprecated/bad practice call!!
    }
}

static void ExecuteWebService(){

    // the details of this are left to the consumer of the method
    int x = WebServiceProxy.CallWebServiceMethodOfInterest();
    // do something fascinating with the result

}

Thread.Abort() の呼び出しは .NET 2.0 以降非推奨であり、主にアンマネージ コードとの有害な相互作用の可能性があるため、一般的に不適切なプログラミング手法と見なされています。もちろん、コードの使用に関連するリスクは、評価する必要があります。

于 2012-09-01T23:34:59.277 に答える