0

サービス内のさまざまなコンポーネントからログファイルを取得する、長時間実行される非同期WCFサービス操作があります。現在、すべてが正常に機能していますが、実装したい「あると便利な」機能がもう1つあります。

WCFは、非同期サービスに時間がかかりすぎるとタイムアウトになりますが、コンポーネントの1つが誤動作している場合、タイムアウト期間に割り当てるよりもログファイルの提供に時間がかかる可能性があります。これが発生した場合、クライアントアプリケーションがユーザーにログファイルの取得に時間がかかることを通知し、ユーザーが待機を続けるかどうかを尋ねると便利です。ユーザーが「はい」と答えた場合、タイムアウトしたときの状態で操作を再開し、タイムアウトタイマーをリセットする方法はありますか?

この擬似コードは、私たちが念頭に置いていることを示しています。

public void GetServiceLogFiles(Action<Object> callback)
{
    try
    {
        var gotLogFilesDelegate = (result) =>
            { var answer = WcfService.EndGetLogFiles(result);
              callback(answer); };
        WcfService.BeginGetLogFiles(gotLogFilesDelegate, null);
    }
    catch(TimeoutException)
    {
        var dialog = new Dialog("The service is taking a while to get the log files. Would you like to keep on waiting for it to finish?");
        if(dialog.response = Dialog.YES)
            Magic_happens_here_and_lets_the_Wcf_Service_continue_working_on_Get_Log_Files();
    }
}
4

1 に答える 1

3

タイムアウト値を設定する方法があります。System.ServiceModel.Channels.Bindingを見てください。これには、次のプロパティがあります。

ReceiveTimeout
OpenTimeout
SendTimeout
CloseTimeout

これらは、サービスプロキシを作成するときに設定できます。

public static class MyWcfServiceProxyFactory
{
    public static MyWcfService CreateMyWcfService(string endpointUrl) 
    {

        EndpointAddress endpointAddress = new EndpointAddress(endpointUrl);
        CustomBinding customBinding = new CustomBinding();

        TimeSpan timeout = new TimeSpan(0, 5, 0);

        customBinding.ReceiveTimeout = timeout;
        customBinding.OpenTimeout = timeout;
        customBinding.SendTimeout = timeout;
        customBinding.CloseTimeout = timeout;

        ChannelFactory<MyWcfService> channelFactory = new ChannelFactory<MyWcfService>(customBinding, endpointAddress);

        return channelFactory.CreateChannel();
    }
}

バインディングがconfigで作成されている場合は、同じ設定を使用できます。

<bindings>
    <basicHttpBinding>
        <binding name="MyWcfService"  
        receiveTimeout="0:05:00"
        openTimeout="0:05:00"
        sendTimeout="0:05:00"
        closeTimeout="0:05:00">

事後にタイムアウトを変更できるとは思わないので、2つのチャネルを作成する必要があります。1つは「通常の」タイムアウトで、もう1つは拡張タイムアウトです。通常の1回がタイムアウトした場合、再試行はタイムアウトが延長されたチャネルを使用できます。

于 2012-07-18T00:36:37.810 に答える