更改WCF默认超时时间

4

我这里有一个WCF双工服务,要求是客户端的回调需要有10秒的超时时间,因此我的服务的web.config文件如下:

        <bindings>
        <basicHttpBinding>
            <binding name="simulatorEndpoint" closeTimeout="00:00:10" openTimeout="00:00:10" 
                receiveTimeout="00:00:10" sendTimeout="00:00:10" 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="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
        <wsDualHttpBinding>
            <binding name="wsdualEndpoint" closeTimeout="00:00:10" openTimeout="00:00:10"
                receiveTimeout="00:00:10" sendTimeout="00:00:10" bypassProxyOnLocal="false"
                transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536" clientBaseAddress="http://localhost:1235"
                messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:00:10"  />
                <security mode="Message">
                    <message clientCredentialType="Windows" negotiateServiceCredential="true"
                        algorithmSuite="Default" />
                </security>
            </binding>
        </wsDualHttpBinding>
    </bindings>

在客户端,app.config文件中的绑定与超时值相同。现在的影响是,如果客户端向服务器发送请求,则超时时间为10秒。但另一方面,如果服务向客户端发送回调,则超时时间为1分钟。这很奇怪...显然客户端上的超时已正确设置...但服务端没有...我该如何更改服务端的超时时间?
PS:我正在使用Visual Studio 2010和它的调试模式,以及适当的ASP.NET Development Server 10.0.0.0。
2个回答

4

关于 绑定超时 的简要概述...

客户端:

  • SendTimeout 用于初始化 OperationTimeout,它控制了整个发送消息的交互过程(包括在请求-响应情况下接收回复消息)。该超时时间也适用于从 CallbackContract 方法发送回复消息。
  • 当没有传递显式超时值时,OpenTimeout 和 CloseTimeout 用于打开和关闭通道。
  • ReceiveTimeout 不使用。

服务器端:

  • 对于回调,Send、Open 和 Close 超时与客户端相同。
  • ReceiveTimeout 由 ServiceFramework 层用于初始化会话空闲超时。

[编辑:一些代码] 还可以尝试将此添加到服务配置中

<behaviors>
   <endpointBehaviors>
      <behavior name="MyCallbackBehavior">       
         <callbackTimeouts transactionTimeout="00:00:10"/>
      </behavior>
   </endpointBehaviors>
<behaviors>

然后将该行为添加到您的端点中。

<endpoint behaviorConfiguration="MyCallbackBehavior" />

当我设置这个行为时,客户端在第一次尝试调用服务上的方法时会卡住,而其他超时(如您在代码片段中看到的)已经设置好了。有可能WCF忽略了我的设置并将所有超时值都设置回默认值吗?是否有一种以编程方式设置服务超时的方法?我在这里读到了一个线程,但解决方案对我来说并不真正有吸引力。但是,我的假设是否正确,即当服务回调客户端时,此超时持续时间与客户端端的超时设置无关? - Hannes S
这似乎是从https://msdn.microsoft.com/en-us/library/hh924831(v=vs.110).aspx直接复制的。当您从在线源直接抄袭时,必须引用该来源。我已经为您修复了它。 - tom redfern

3

好的,我找到了错误...

我正确地使用了bindingConfiguration

        <wsDualHttpBinding>
        <binding name="wsdualEndpoint" closeTimeout="00:00:10" openTimeout="00:00:10"
            receiveTimeout="00:00:10" sendTimeout="00:00:10" bypassProxyOnLocal="false"
            transactionFlow="false" hostNameComparisonMode="StrongWildcard"
            maxBufferPoolSize="524288" maxReceivedMessageSize="65536" clientBaseAddress="http://localhost:1235"
            messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                maxBytesPerRead="4096" maxNameTableCharCount="16384" />
            <reliableSession ordered="true" inactivityTimeout="00:00:10"  />
            <security mode="Message">
                <message clientCredentialType="Windows" negotiateServiceCredential="true"
                    algorithmSuite="Default" />
            </security>
        </binding>
    </wsDualHttpBinding>

但是关键在于这是我的终端声明:
        <endpoint address="dual" binding="wsDualHttpBinding" 
      name="wsdualEndpoint" contract="INotificationService"/>

因为我的假设是他会获取上述定义的绑定配置并将其用于我的端点,但这是错误的,我必须在端点声明中添加bindingConfiguration="THE NAME of THE CONFIGURATION"。

因此,仅供参考,我的工作配置如下:

      <wsDualHttpBinding>
    <binding name="MywsdualEndpoint" sendTimeout="00:00:05" bypassProxyOnLocal="false"
        transactionFlow="false" hostNameComparisonMode="StrongWildcard"
        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
          maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true"/>
      <security mode="Message">
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
            algorithmSuite="Default" />
      </security>
    </binding>
  </wsDualHttpBinding>

正确的终端声明为:

        <endpoint address="dual" binding="wsDualHttpBinding" bindingConfiguration="MywsdualEndpoint" contract="INotificationService"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接