2012-07-17 66 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个通道,一个与“正常”超时和一个具有扩展超时。如果正常超时,重试尝试可以使用具有扩展超时的通道。