2012-07-17 62 views
0

我們有一個長時間運行的異步WCF服務操作,它從我們的服務中的不同組件抓取日誌文件。現在,一切工作正常,但還有一個「我們想要實現的功能」。如何讓WCF服務在超時後完成更多時間?

如果WCF將花費太長時間,它將超時異步服務,但如果我們的某個組件運行異常,則可能需要更長的時間才能發佈其日誌文件,而不是超時。如果發生這種情況,如果客戶端應用程序告訴用戶獲取日誌文件需要一段時間,並詢問用戶是否想繼續等待,那將會很好。如果用戶說是,有什麼方法可以在超時時重啓超時定時器的狀態下恢復操作?

這個僞代碼顯示了我們心目中:

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(); 
    } 
} 

回答

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個通道,一個與「正常」超時和一個具有擴展超時。如果正常超時,重試嘗試可以使用具有擴展超時的通道。