简体   繁体   English

WCF服务,如何增加超时?

[英]WCF Service , how to increase the timeout?

Might seem like a silly question, but everything in WCF seems a lot more complicated than in asmx, how can I increase the timeout of an svc service? 可能看起来像一个愚蠢的问题,但WCF中的所有内容似乎都比asmx复杂得多,我怎样才能增加svc服务的超时?

Here is what I have so far: 这是我到目前为止:

<bindings>
      <basicHttpBinding>
        <binding name="IncreasedTimeout" 
          openTimeout="12:00:00" 
          receiveTimeout="12:00:00" closeTimeout="12:00:00"
          sendTimeout="12:00:00">
        </binding>
      </basicHttpBinding>
</bindings>

And then my endpoint gets mapped like this: 然后我的端点映射如下:

<endpoint address="" 
  binding="basicHttpBinding" bindingConfiguration="IncreasedTimeout"
             contract="ServiceLibrary.IDownloads">
             <identity>
                <dns value="localhost" />
             </identity>
          </endpoint>

The exact error I am getting: 我得到的确切错误:

The request channel timed out while waiting for a reply after 00:00:59.9990000. 在00:00:59.9990000之后等待回复时,请求通道超时。 Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. 增加传递给Request的调用的超时值或增加Binding上的SendTimeout值。 The time allotted to this operation may have been a portion of a longer timeout. 分配给此操作的时间可能是较长超时的一部分。

In the WCF Test Client, there is a config icon that contains the run time configuration of my service: 在WCF测试客户端中,有一个配置图标,其中包含我的服务的运行时配置:

As you can see its not the same values as I've set for it? 你可以看到它与我为它设定的值不一样吗? What am I doing wrong? 我究竟做错了什么?

<bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IDownloads" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="">
                            <extendedProtectionPolicy policyEnforcement="Never" />
                        </transport>
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>

In your binding configuration, there are four timeout values you can tweak: 在绑定配置中,您可以调整四个超时值:

<bindings>
  <basicHttpBinding>
    <binding name="IncreasedTimeout"
             sendTimeout="00:25:00">
    </binding>
  </basicHttpBinding>

The most important is the sendTimeout , which says how long the client will wait for a response from your WCF service. 最重要的是sendTimeout ,它表示客户端等待WCF服务响应的时间。 You can specify hours:minutes:seconds in your settings - in my sample, I set the timeout to 25 minutes. 您可以在设置中指定hours:minutes:seconds - 在我的示例中,我将超时设置为25分钟。

The openTimeout as the name implies is the amount of time you're willing to wait when you open the connection to your WCF service. 名称暗示的openTimeout是您打开与WCF服务的连接时愿意等待的时间。 Similarly, the closeTimeout is the amount of time when you close the connection (dispose the client proxy) that you'll wait before an exception is thrown. 类似地, closeTimeout是您关闭在抛出异常之前等待的连接( closeTimeout客户端代理)的时间。

The receiveTimeout is a bit like a mirror for the sendTimeout - while the send timeout is the amount of time you'll wait for a response from the server, the receiveTimeout is the amount of time you'll give you client to receive and process the response from the server. receiveTimeout有点像在镜子sendTimeout -而发送超时的时候,你会等待来自服务器的响应量, receiveTimeout是的时候,你会给你的客户接受和处理量来自服务器的响应。

In case you're send back and forth "normal" messages, both can be pretty short - especially the receiveTimeout , since receiving a SOAP message, decrypting, checking and deserializing it should take almost no time. 如果您来回发送“正常”消息,两者都可能非常短 - 尤其是receiveTimeout ,因为接收SOAP消息,解密,检查和反序列化消息几乎不需要时间。 The story is different with streaming - in that case, you might need more time on the client to actually complete the "download" of the stream you get back from the server. 故事与流式传输不同 - 在这种情况下,您可能需要更多时间在客户端上实际完成从服务器返回的流的“下载”。

There's also openTimeout, receiveTimeout, and closeTimeout. 还有openTimeout,receiveTimeout和closeTimeout。 The MSDN docs on binding gives you more information on what these are for. 有关绑定MSDN文档为您提供了有关这些内容的更多信息。

To get a serious grip on all the intricasies of WCF, I would strongly recommend you purchase the " Learning WCF " book by Michele Leroux Bustamante: 为了严格控制WCF的所有复杂性,我强烈建议您购买Michele Leroux Bustamante撰写的“ 学习WCF ”一书:

Learning WCF http://ecx.images-amazon.com/images/I/51GNuqUJq%2BL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg 学习WCF http://ecx.images-amazon.com/images/I/51GNuqUJq%2BL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg

and you also spend some time watching her 15-part " WCF Top to Bottom " screencast series - highly recommended! 你还花了一些时间看她的15部分“ WCF Top to Bottom ”截屏系列 - 强烈推荐!

For more advanced topics I recommend that you check out Juwal Lowy's Programming WCF Services book. 对于更高级的主题,我建议您查看Juwal Lowy的编程WCF服务手册。

Programming WCF http://ecx.images-amazon.com/images/I/41odWcLoGAL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg 编程WCF http://ecx.images-amazon.com/images/I/41odWcLoGAL._BO2,204,203,200_PIsitb-sticker-arrow-click,TopRight,35,-76_AA240_SH20_OU01_.jpg

需要在客户端级别设置超时配置,因此我在web.config中设置的配置无效,WCF测试工具有自己的配置,您需要设置超时。

The best way is to change any setting you want in your code. 最好的方法是在代码中更改所需的任何设置。

Check out the below example: 看看下面的例子:

using(WCFServiceClient client = new WCFServiceClient ())
{ 
    client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 1, 30);
}

Got the same error recently but was able to fixed it by ensuring to close every wcf client call. 最近得到了同样的错误但是能够通过确保关闭每个wcf客户端调用来修复它。 eg. 例如。

WCFServiceClient client = new WCFServiceClient ();
//More codes here
// Always close the client.
client.Close();

or 要么

using(WCFServiceClient client = new WCFServiceClient ())
{ 
    //More codes here 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM