如何在WCF中为HTTP连接设置保持活动间隔

时间:2010-08-12 09:55:47

标签: .net wcf

WCF中的Http传输通道默认使用持久HTTP连接。如何控制这些连接的活动超时?默认值为100秒。我通过监控Procmon中的应用程序找到了这个值。我没有在http传输绑定元素中找到配置此超时的任何设置。是否有可以控制超时的.NET类?

6 个答案:

答案 0 :(得分:8)

答案 1 :(得分:1)

听起来我想要调整Keep-Alive HTTP标头。你应该在HTTP keep-alive上阅读this article,然后首先确定这是否真的值得花时间。

如果是,请尝试创建Message Inspector。这应该允许您修改发送的每条消息的HTTP标头:

public class KeepAliveMessageInspector : IClientMessageInspector
{
    // ...

    public object BeforeSendRequest(
        ref System.ServiceModel.Channels.Message request,
        System.ServiceModel.IClientChannel channel)
    {
        // Add/modify the Keep-Alive header
        var httpRequestMessage = new HttpRequestMessageProperty();
        httpRequestMessage.Headers.Add("Keep-Alive", "9000");
        request.Properties.Add(
            HttpRequestMessageProperty.Name, httpRequestMessage);
        return null;
    }

    // ...
}

答案 2 :(得分:1)

我找到了解决方法。问题是底层内核http.sys有自己的超时,它会破坏连接。

http://mmmreddy.wordpress.com/2013/07/11/wcf-use-of-http-transport-sharing-persistent-tcp-sessions/

netsh http add timeout timeouttype=idleconnectiontimeout value=120

此外,此问题与此What 130 second timeout is killig my WCF streaming service call?

类似

答案 3 :(得分:1)

设置ServicePoint.MaxIdleTime应该允许您更改持久HTTP连接上的默认100秒空闲超时。

https://www.visualbasicplanet.info/windows-communication/configuring-http-connections.html

答案 4 :(得分:0)

this answer开始,我可以阅读here,看起来您想要创建自定义http绑定,并设置inactivityTimeout属性。

来自MSDN文章:

<bindings>
  <customBinding>
    <binding name="Binding1">
      <reliableSession acknowledgementInterval="00:00:00.2000000" enableFlowControl="true"
                        maxTransferWindowSize="32" inactivityTimeout="00:10:00" maxPendingChannels="128"
                        maxRetryCount="8" ordered="true" />
      <security mode="None"/>
      <httpTransport authenticationScheme="Anonymous" bypassProxyOnLocal="false"
                    hostNameComparisonMode="StrongWildcard" 
                    proxyAuthenticationScheme="Anonymous" realm="" 
                    useDefaultWebProxy="true" />
    </binding>
  </customBinding>
</bindings>

答案 5 :(得分:0)

这个怎么样?似乎它可能是您的IIS服务器的功能,而与WCF服务无关?我知道这个链接适用于IIS6,但也许它可以作为IIS7中类似内容的基础? http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/73566f83-c257-4941-8ed8-7ae45b2e7985.mspx?mfr=true