How to set keep alive interval for HTTP connection in WCF
Asked Answered
S

6

17

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 by monitoring application in Procmon. I haven't found any setting in http transport binding element which configures this timeout. Is there any .NET class which can control that timeout?

Strephon answered 12/8, 2010 at 9:55 Comment(0)
B
8

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.

Barrus answered 22/8, 2010 at 23:26 Comment(1)
Thanks for post but this doesn't answer the question. Question is not about turning off keep alive but about setting keep alive timeout. So I want to have keep alive turn on and I want to control its duration.Strephon
A
2

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?

Acetify answered 27/8, 2013 at 15:44 Comment(0)
A
1

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

    // ...
}
Asp answered 25/8, 2010 at 18:52 Comment(4)
This idea is interesting. I saw something like that in communication with Cassini but I think it is not standard header. Anyway I will try it during next few days. If it works I will set the new bounty and award you.Strephon
Btw. I don't need to use it. I just want to know if it is possible to change it somehow and where the default value 100s comes from.Strephon
@Ladislav Mrnka : Sorry to burry out this old post, but have you been able to set or read this HTTP keep-alive value. If yes, I would be very interested in knowing more about it. ThanksCalycle
io.com/~maus/HttpKeepAlive.html not found 404 error, url brokenVassili
L
1

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

Lacework answered 8/5, 2018 at 14:3 Comment(0)
M
0

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>
Message answered 24/8, 2010 at 14:12 Comment(3)
No. I want to set HTTP keep alive not Reliable session keep alive. HTTP keep alive is described in HTTP protocol specification.Strephon
Seems like HTTP connections should always be persistent, as per RFC 2616 (w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.1.1).Message
I don't think so. The RFC says that HTTP implementation should implement HTTP persistent connections. Not that only persistent connection should be used.Strephon
U
0

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

Undesigned answered 25/8, 2010 at 18:41 Comment(1)
I have already tryed to set up connection timeout on IIS and it didn't work. It is not related to keep alive setting. Anyway I will try it again during next few days. If it works I will set the new bounty and award you.Strephon

© 2022 - 2024 — McMap. All rights reserved.