WCF服务超时

时间:2012-03-05 14:08:02

标签: wcf timeout

有没有办法在服务端设置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 -->

4 个答案:

答案 0 :(得分:8)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

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

Binding.ReceiveTimeout

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

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

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

  

HttpRuntimeSection.ExecutionTimeout

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

答案 3 :(得分:0)

是的,我可以处理它你必须配置你的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>

   - &GT;    - &GT;

您的客户端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>