WCF超时问题

本文关键字:问题 超时 WCF | 更新日期: 2023-09-27 18:20:04

我们有一个应用程序,它将对象(在本例中为"printJob")发送到服务。根据我们的交易量,这个过程可能需要一段时间。一旦服务完成了它所做的,它就会向应用程序返回一个"PrintJobID"。

PrintGatewayClient printClient = new PrintGatewayClient();
PrintService.ServiceJob printJob = new PrintService.ServiceJob();
printJob.ServicePrintItems = checkList.ToArray();
try
{
    currentBatch.PrintJobID = printClient.SubmitPrintJob(printJob);
    PaymentBatchesGateway.Update(currentBatch);
}
catch (System.Exception ex)
{
    throw ex;
}
printClient.Close();

应用程序等待并等待printClient完成其作业并接收整数"PrintJobID",然后用该ID更新表。然而,当运行大批量时,我们会得到以下错误:

The request channel timed out while waiting for a reply after 00:04:59.9062464. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.

我已经在netTcpBinding:上查看了我们的web.config文件

<netTcpBinding>
    <binding name="NetTcpBinding_IPrintGateway" closeTimeout="00:01:00"
       openTimeout="00:01:00" receiveTimeout="02:00:00" sendTimeout="02:00:00"
       transactionFlow="false" transferMode="Buffered"
       transactionProtocol="OleTransactions"
       hostNameComparisonMode="StrongWildcard" listenBacklog="10"
       maxBufferPoolSize="524288" maxBufferSize="50000000" maxConnections="10"
       maxReceivedMessageSize="50000000">
    <readerQuotas maxDepth="5000" maxStringContentLength="150000"
       maxArrayLength="16384" maxBytesPerRead="50000000" 
       maxNameTableCharCount="16384" />
    <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
    <security mode="None">
            <transport clientCredentialType="None" protectionLevel="None" />
            <message clientCredentialType="None" />
    </security>
    </binding>
<netTcpBinding>
<client>
    <endpoint address="http://server/printgateway/PrintGateway.svc"
        binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IPrintGateway"
        contract="PrintService.IPrintGateway" 
        name="BasicHttpBinding_IPrintGateway" />    
</client>

我在代码中找不到00:05:00以外的任何时间。谁能解释一下并告诉我如何延长这段时间吗。我可以在服务器上的web.config中的代码中执行此操作吗?

谢谢!

WCF超时问题

让服务调用上的线程块等待响应甚至60秒都是多余的,并且代表了糟糕的设计选择。你当然不想把时间增加到5分钟,如果是5分钟,为什么不增加一个小时呢?这不是一个现实的或可支持的解决方案。

您最好改用异步服务调用。

当您将服务引用添加到项目中时,在"添加服务引用"对话框中,单击"高级",然后在"客户端"下选择"生成异步操作"。由此生成的代理类将为服务协定中的每个OperationName具有[OperationName]Completed事件和[OperationName]Async方法。

  var client = new Service1Client();
  client.GetDataCompleted += Client_GetDataCompleted; // specify the callback method
  client.GetDataAsync( 0 );
  // ...

static void Client_GetDataCompleted( object sender, GetDataCompletedEventArgs e )
{
  var response = e.Result;
  // ...
}

一旦WCF基础结构接收到响应消息,就会在线程上调度回调方法。更多信息,请参阅"如何:异步调用WCF服务操作"http://msdn.microsoft.com/en-us/library/ms730059.aspx.

有两个配置文件,一个在服务端,另一个在使用者端。看起来你有消费者配置。您可能会在服务目录中找到5分钟的超时设置。

但是,我同意Visual Stuart的观点,即同步服务调用的长时间等待是不可取的。应该做的是将打印作业添加到队列中,在处理完这些作业后,可以向调用的使用者发送响应。

然而,这将是一项重大的重建工作。

我们在Silverlight控件启动业务逻辑时遇到过类似的情况,根据所选择的操作,会导致超时。我实现了一个轮询场景,例如低于

  1. 使用StartOperation调用的WCF服务方法,该方法向使用者返回了唯一的ID
  2. 服务方法启动一个后台进程,该进程完成工作并简单地报告状态
  3. 消费者使用唯一ID调用单独的wcf服务状态方法来获取状态
  4. Status方法检查后台进程(我使用了一个singleton管理器,它在查找字典中有正在运行的进程)并返回其状态
  5. 消费者拨打#3直到完成报告
  6. singleton管理器可以在流程报告完成后清理流程,和/或允许使用者报告其完成并将其清理