首页 文章

WCF服务,如何增加超时?

提问于
浏览
83

可能看起来像一个愚蠢的问题,但WCF中的所有内容似乎都比asmx复杂得多,我怎样才能增加svc服务的超时?

这是我到目前为止:

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

然后我的 endpoints 映射如下:

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

我得到的确切错误:

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

在WCF测试客户端中,有一个配置图标,其中包含我的服务的运行时配置:

你可以看到它与我为它设定的值不一样吗?我究竟做错了什么?

<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>

4 回答

  • 4

    在绑定配置中,您可以调整四个超时值:

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

    最重要的是 sendTimeout ,它表示客户端等待WCF服务响应的时间 . 您可以在设置中指定 hours:minutes:seconds - 在我的示例中,我将超时设置为25分钟 .

    顾名思义, openTimeout 是您打开与WCF服务的连接时愿意等待的时间 . 类似地, closeTimeout 是在抛出异常之前关闭连接(处置客户端代理)的时间 .

    receiveTimeout 有点像 sendTimeout 的镜像 - 而发送超时是您等待服务器响应的时间, receiveTimeout 是您给客户端接收和处理响应的时间 . 来自服务器的响应 .

    如果你来回发送消息,两者都可能很短 - 特别是 receiveTimeout ,因为接收SOAP消息,解密,检查和反序列化它几乎不需要时间 . 故事与流式传输不同 - 在这种情况下,您可能需要更多时间在客户端上实际完成从服务器返回的流的"download" .

    还有openTimeout,receiveTimeout和closeTimeout . MSDN docs on binding为您提供了有关这些内容的更多信息 .

    为了严格控制WCF的所有复杂性,我强烈建议您购买Michele Leroux Bustamante撰写的“Learning 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

    你也花了一些时间看她的15部分“WCF Top to Bottom”截屏系列 - 强烈推荐!

    对于更高级的主题,我建议您查看Juwal Lowy的Programming 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

  • 167

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

  • 17

    最好的方法是在代码中更改所需的任何设置 .

    看看下面的例子:

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

    最近得到了同样的错误但是能够通过确保关闭每个wcf客户端调用来修复它 . 例如 .

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

    要么

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

相关问题