How to set keep alive interval for HTTP connection in WCF

后端 未结 6 1164
一向
一向 2020-12-03 03:45

Http transport channel in WCF uses persistent HTTP connections by default. How to control keep alive timeout for those connections? Default value is 100s. I found that value

相关标签:
6条回答
  • 2020-12-03 03:58

    Take a look here:

    http://web.archive.org/web/20080721055556/http://blog.magenic.com/blogs/jons/archive/2007/05/04/The-Tao-of-Microsoft-WCF-CustomBinding-Configuration-Elements.aspx

    There is a detailed discussion of manipulating the keep alive property during an http connection.

    0 讨论(0)
  • 2020-12-03 04:00

    I found the solution for this. The problem is that the underlying kernel http.sys has it's own timeout and it will break the connection.

    http://mmmreddy.wordpress.com/2013/07/11/wcf-use-of-http-transport-sharing-persistent-tcp-sessions/
    
    netsh http add timeout timeouttype=idleconnectiontimeout value=120
    

    Also, this question is similar to this What 130 second timeout is killig my WCF streaming service call?

    0 讨论(0)
  • 2020-12-03 04:01

    From this answer, and what I can read here, it looks like you want to create a custom http binding, and set the inactivityTimeout attribute.

    From the MSDN article:

    <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>
    
    0 讨论(0)
  • 2020-12-03 04:02

    It sounds to me like you're wanting to adjust the Keep-Alive HTTP header. You should read this article on HTTP keep-alive and first determine if this is really something worth your time.

    If it is, try creating a Message Inspector. This should allow you to modify the HTTP headers for each message that gets sent out:

    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;
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-12-03 04:08

    How about this? Seems it may be a function of your IIS Server and nothing to do with the WCF service? I know this link applies to IIS6, but maybe it serves as a foundation to something similar in IIS7? http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/73566f83-c257-4941-8ed8-7ae45b2e7985.mspx?mfr=true

    0 讨论(0)
  • 2020-12-03 04:14

    Setting ServicePoint.MaxIdleTime should let you change the default 100 second idle timeout on the persistent HTTP connections.

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

    0 讨论(0)
提交回复
热议问题