在 C# 中增加 WCF Web 服务的超时时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9099045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:55:52  来源:igfitidea点击:

Increasing timeout for WCF web service in c#

c#wcf

提问by user1183014

I currently have an application that is calling a web service on a server for searching. We can expect a large amount of data to be returned, so a search to take longer than a minute is routine.

我目前有一个应用程序正在调用服务器上的 Web 服务进行搜索。我们可以预期会返回大量数据,因此需要超过一分钟的搜索时间是例行公事。

We've been receiving the below error message for such large volume searches:

对于如此大量的搜索,我们一直收到以下错误消息:

The request channel timed out while waiting for a reply after 00:00:59.7350618. 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.

请求通道在 00:00:59.7350618 之后等待回复时超时。增加传递给 Request 调用的超时值或增加 Binding 上的 SendTimeout 值。分配给此操作的时间可能是较长超时的一部分。

This is something that we've seen asked in multiple questions already posted on StackOverflow, unfortunately none of the available solutions have helped me fix the issue or even be able to configure the time out window.

这是我们已经在 StackOverflow 上发布的多个问题中看到的问题,不幸的是,没有可用的解决方案帮助我解决问题,甚至无法配置超时窗口。

We've both changed the app.config for the client, increased all the timeouts involved there (CloseTimeout, OpenTimeout, ReceiveTimeout, and SendTimeout) and all web.config values for the service on the server (closeTimeout, openTimeout, and SendTimeout).

我们都更改了客户端的 app.config,增加了其中涉及的所有超时(CloseTimeout、OpenTimeout、ReceiveTimeout 和 SendTimeout)以及服务器上服务的所有 web.config 值(closeTimeout、openTimeout 和 SendTimeout)。

None of these changes have had any effect, I still receive the minute timeout. Any idea why changes to these values would have no effect?

这些更改都没有产生任何影响,我仍然收到分钟超时。知道为什么更改这些值没有效果吗?

In the examples below, we lowered the time to keep us from having to wait for the full minute during testing.

在下面的示例中,我们缩短了时间,使我们不必在测试期间等待整整一分钟。

Web.config:

网页配置:

<configuration>
  <system.web>
    <compilation targetFramework="4.0" />
  </system.web>
  <system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.Net">
        <listeners>
          <add name="TraceFile" />
        </listeners>
      </source>
      <source name="System.Net.Sockets">
        <listeners>
          <add name="TraceFile" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="TraceFile" type="System.Diagnostics.TextWriterTraceListener"
           initializeData="trace.log" />
    </sharedListeners>
    <switches>
      <add name="System.Net" value="Verbose" />
      <add name="System.Net.Sockets" value="Verbose" />
    </switches>
  </system.diagnostics>
  <system.serviceModel>
    <diagnostics>
      <messageLogging logMalformedMessages="false" logMessagesAtServiceLevel="false"
                      logMessagesAtTransportLevel="false" />
    </diagnostics>
    <services>
      <service behaviorConfiguration="SearchIndexServiceBehavior" name="SearchIndex.Service">
        <endpoint address="" binding="basicHttpBinding" contract="ISearchIndexServices" />
        <host>
          <timeouts closeTimeout="00:00:10" />
        </host>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding closeTimeout="00:00:10" openTimeout="00:00:15" sendTimeout="00:00:20"
                 receiveTimeout="00:00:25" maxBufferSize="2147483647" maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="2147483647"
                        maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="SearchIndexServiceBehavior">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <system.transactions>
    <defaultSettings timeout="00:05:00" />
  </system.transactions>
</configuration>

app.config

应用程序配置文件

<configuration>
  <configSections>
  </configSections>
  <startup>
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISearchIndexServices" closeTimeout="00:00:10" openTimeout="00:00:15" receiveTimeout="00:10:00" sendTimeout="00:00:20" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="5242880" maxBufferPoolSize="524288" maxReceivedMessageSize="5242880" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="5242880"maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://MyServer/SearchIndexService/SearchIndexServices.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISearchIndexServices" contract="WFC.ISearchIndexServices" name="BasicHttpBinding_ISearchIndexServices" />
    </client>
  </system.serviceModel>
</configuration>

回答by Richard Blewett

The timeout is the send timeout on the client. This is how long the client is prepared to wait for a response and so has to be set on the client rather than the service

超时是客户端上的发送超时。这是客户端准备等待响应的时间,因此必须在客户端而不是服务上设置

回答by BuzzWilder

This Service Model example shows the binding element timeout properties that needed to be set in the client web.config.

此服务模型示例显示了需要在客户端 web.config 中设置的绑定元素超时属性。

Notice that all of the timeout properties are set to 10 minutes. This example uses a binding configurationelement to set the "maxItemsInObjectGraph"property. Usually if you need to turn the timeout up, that means you are probably transferring large amounts of data. Notice also that the sizes of the data to be transferrred are all set to handle up to 2 gigabytes.

请注意,所有超时属性都设置为 10 分钟。此示例使用绑定配置元素来设置“maxItemsInObjectGraph”属性。通常,如果您需要调高超时时间,则意味着您可能正在传输大量数据。另请注意,要传输的数据大小都设置为最多可处理 2 GB。

<?xml version="1.0" encoding="UTF-8"?>
<system.serviceModel>
   <bindings>
      <basicHttpBinding>
         <binding name="BasicHttpBinding_ISearchIndexServices" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
            <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
            <security mode="None">
               <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
               <message clientCredentialType="UserName" algorithmSuite="Default" />
            </security>
         </binding>
      </basicHttpBinding>
   </bindings>
   <client>
      <endpoint address="http://MyServer/SearchIndexService/SearchIndexServices.svc" behaviorConfiguration="SearchIndexServicesBehavior" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISearchIndexServices" contract="WFC.ISearchIndexServices" name="BasicHttpBinding_ISearchIndexServices" />
   </client>
   <behaviors>
      <endpointBehaviors>
         <behavior name="SearchIndexServicesBehavior">
            <dataContractSerializer maxItemsInObjectGraph="2147483647" />
         </behavior>
      </endpointBehaviors>
   </behaviors>
</system.serviceModel>

回答by Chris Dickson

I think you are probably hitting the OperationTimeout for the client side request channel, which for some reason is not easily adjusted via the standard configuration attributes.

我认为您可能正在为客户端请求通道点击 OperationTimeout,由于某种原因,它不容易通过标准配置属性进行调整。

Try this in the client code before calling the long-running operation:

在调用长时间运行的操作之前,在客户端代码中试试这个:

((IContextChannel)clientProxy.InnerChannel).OperationTimeout = new TimeSpan(0,30,0); // For 30 minute timeout - adjust as necessary

where clientProxyis an instance of the service-reference-generated Client class (which is derived from ClientBase<ISearchIndexService>).

其中clientProxy是服务引用生成的 Client 类(派生自ClientBase<ISearchIndexService>)的实例。

回答by Dinesh Kumar

You can try this

你可以试试这个

 SeriveClient client=new ServiceClient();
    var time = new TimeSpan(0, 3, 0);
    client.Endpoint.Binding.CloseTimeout = time;
                client.Endpoint.Binding.OpenTimeout = time;
                client.Endpoint.Binding.ReceiveTimeout = time;
                client.Endpoint.Binding.SendTimeout = time;

回答by Keenan Stewart

Timeouts are a pain in debugging. Who can debug in under a minute! I used Junior M/BuzzWilder from above and added the following to the service call in the app.config:

超时是调试中的痛苦。谁能在一分钟内调试!我从上面使用了 Junior M/BuzzWilder,并将以下内容添加到 app.config 中的服务调用中:

            <binding name="WSHttpBinding_bindingname"
                     openTimeout="01:00:00"
                     closeTimeout="01:00:00"
                     receiveTimeout="01:00:00"
                     sendTimeout="01:00:00"
                     >

Good bye service time outs. :)

再见服务超时。:)