WCF 服务超时

Posted

技术标签:

【中文标题】WCF 服务超时【英文标题】:WCF service timeout 【发布时间】:2012-03-23 00:06:43 【问题描述】:

有没有办法在服务端设置一个 tiemout 以便请求在超过超时时停止处理?我知道我可以在客户端将请求超时,但这不会停止服务器上对请求的处理。

我已尝试添加以下绑定:

<basicHttpBinding>
    <binding name="timeout" receiveTimeout="00:01:00" closeTimeout="00:01:00" openTimeout="00:01:00" sendTimeout="00:01:00" />
</basicHttpBinding>

我还尝试在 system.web 节点中添加以下内容(单独和与上述内容一起):

<httpRuntime executionTimeout="60" /> <!-- timeout after 60 seconds -->

【问题讨论】:

您可以在 Endpoint 的绑定配置上配置的超时主要与传输/通道超时有关,您所要求的是一种方法来限制您的服务处理代码的时间,而不管有多少通信时间是必要的吧?例如,您想设置您的 WCF 服务将在处理操作超过 2 分钟时中止...这是您要问的吗? @Cshapenter - 这基本上是正确的。 【参考方案1】:

是的,我可以处理它,你必须配置你的 web.config 文件,它看起来像

<?xml version="1.0" encoding="UTF-8"?>

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
        <add name="DomainServiceModule" preCondition="managedHandler" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </modules>
    <directoryBrowse enabled="false" />
</system.webServer>

<system.web>
    <httpModules>
        <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </httpModules>
    <compilation debug="true" targetFramework="4.0"> 
      <assemblies><add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  <httpRuntime executionTimeout="36000"/>
  <!--<sessionState mode="InProc" timeout="36000" />-->
</system.web>


<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

  <bindings>
    <basicHttpBinding>
      <binding name="Sbinding" maxReceivedMessageSize="1500000000" maxBufferSize="1500000000">
        <readerQuotas maxArrayLength="1500000000" maxStringContentLength="1500000000" />
      </binding>
    </basicHttpBinding>

    <webHttpBinding>
      <binding name="Ubinding" maxBufferSize="1500000000" maxBufferPoolSize="1500000000" maxReceivedMessageSize="1500000000" transferMode="Streamed">
        <readerQuotas maxStringContentLength="1500000000" maxArrayLength="1500000000" maxBytesPerRead="1500000000" maxNameTableCharCount="1500000000" />
      </binding>
    </webHttpBinding>

  </bindings>

  <behaviors>
     <serviceBehaviors>
            <behavior name="ClientUpload.Web.UploadService">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>

          <behavior name="ServiceBehaviour">
            <serviceMetadata httpGetEnabled="true" />
            <serviceDebug includeExceptionDetailInFaults="false" />
          </behavior>
    </serviceBehaviors>

    <endpointBehaviors>
      <behavior name="web">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>


  <services>
    <service behaviorConfiguration="ClientUpload.Web.UploadService" name="ClientUpload.Web.Services.UploadService">
      <endpoint address="" binding="basicHttpBinding" bindingConfiguration="Sbinding" contract="ClientUpload.Web.Services.IUploadService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    </service>

    <service name="ClientUpload.Web.Services.RestService" behaviorConfiguration="ServiceBehaviour">
      <endpoint address="Rest" binding="webHttpBinding" contract="ClientUpload.Web.Services.IService1" behaviorConfiguration="web" bindingConfiguration="Ubinding">
      </endpoint>
    </service>
  </services>             


</system.serviceModel>

--> -->

您的客户端 ServiceReferences.ClientConfig 文件看起来像

<configuration>
<system.serviceModel>

    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IUploadService" closeTimeout="00:10:00"
                openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
                maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
            <binding name="BasicHttpBinding_IUploadService1" closeTimeout="00:10:00"
                openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
                maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>

        </basicHttpBinding>
    </bindings>

  <!--<client>
    <endpoint address="http://localhost/ClientUpload.Web_deploy/Services/UploadService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUploadService"
      contract="ServiceReference1.IUploadService" name="BasicHttpBinding_IUploadService" />
  </client>-->


  <client>
    <endpoint address="http://localhost:50503/Services/UploadService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUploadService1"
      contract="ServiceReference.ClientUpload.Web.Services.UploadService.IUploadService"
      name="BasicHttpBinding_IUploadService1" />
    <endpoint address="http://localhost:50503/Services/UploadService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUploadService"
      contract="ServiceReference1.IUploadService" name="BasicHttpBinding_IUploadService" />
  </client>

  <!--<client>
    <endpoint address="http://10.223.211.37:81/ClientUpload/Services/UploadService.svc"
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUploadService"
      contract="ServiceReference1.IUploadService" name="BasicHttpBinding_IUploadService" />        
  </client>-->      

</system.serviceModel>

【讨论】:

【参考方案2】:

没有内置的(开箱即用的)方法可以做到这一点。您可以设置的所有超时都与传输设置有关。简而言之,你必须自己做。

有关限制 WCF 执行时间的信息,另请参阅 this answer。

【讨论】:

谢谢。那么,可能需要等待以后的项目,因为我没有时间为当前项目获取自定义解决方案。【参考方案3】:

我们可以在“绑定”中设置服务器端超时:

Binding.ReceiveTimeout

这是指定服务从开始接收请求到处理消息可以等待多长时间的超时。这是服务器端设置。当您向服务发送大消息并且服务需要较长时间来处理时,您需要增加此设置。

http://msdn.microsoft.com/en-us/library/ms731361.aspx

使用这两个超时应该可以解决大多数超时问题。但是,当 WCF 服务托管在 IIS/ASP.NET 中时,另一个设置也将控制请求的生命周期:

HttpRuntimeSection.ExecutionTimeout

<configuration>
  <system.web>
  <httpRuntime executionTimeout="600"/>
  </system.web>
</configuration>

【讨论】:

查看我最近的编辑 - 这似乎并没有限制从开始到结束完整服务调用的时间,这是我正在尝试做的。 AFAIK 这不是真的。有关ReceiveTimeout 的更多信息,请参阅rauch.io/2015/06/25/all-wcf-timeouts-explained【参考方案4】:

您可以在服务绑定中进行设置,下面的链接显示了要在服务端和客户端设置的值。

http://geekswithblogs.net/smyers/archive/2011/10/05/wcf-service-message-timeouts-size-limits-tips-and-tricks.aspx

【讨论】:

这篇文章不错,但是在绑定上设置超时似乎不起作用。也没有设置 httpRuntime executionTimeout。

以上是关于WCF 服务超时的主要内容,如果未能解决你的问题,请参考以下文章

WCF 服务超时

WCF服务,如何增加超时?

超时 WCF 服务

增加C#类库(DLL)调用wcf服务超时

WCF 超时客户端与服务器

mysql 视图中的 WCF 数据服务超时问题