tags:

views:

356

answers:

4

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?

+3  A: 

Take a look here: 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.

Alison
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.
Ladislav Mrnka
A: 

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>
louisgab
No. I want to set HTTP keep alive not Reliable session keep alive. HTTP keep alive is described in HTTP protocol specification.
Ladislav Mrnka
Seems like HTTP connections should always be persistent, as per RFC 2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html#sec8.1.1).
louisgab
I don't think so. The RFC says that HTTP implementation should implement HTTP persistent connections. Not that only persistent connection should be used.
Ladislav Mrnka
A: 

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

Shawn de Wet
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.
Ladislav Mrnka
A: 

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

    // ...
}
Jacob
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.
Ladislav Mrnka
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.
Ladislav Mrnka