13

I have a WCF client which uses a wsHttpBinding, I would like to enable http keep-alive.

I'm hoping I can turn this on by just changing the client config... I've found plenty of descriptions of how to turn on keep-alives for a basicHttp binding, but no luck with wsHttpBinding... is this possible?

Many thanks.

Here's my client binding:

  <wsHttpBinding>
    <binding name="WSHttpBinding_IRepositoryService" closeTimeout="00:00:10"
      openTimeout="00:00:10" receiveTimeout="00:05:00" sendTimeout="00:05:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="655360" messageEncoding="Mtom"
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="81920" maxArrayLength="163840"
        maxBytesPerRead="409600" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="true" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="">
          <extendedProtectionPolicy policyEnforcement="Never" />
        </transport>
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" establishSecurityContext="true" />
      </security>

    </binding>
</wsHttpBinding>
Nathan
  • 6,095
  • 2
  • 35
  • 61

2 Answers2

25

You'll have to use a custom binding in order to disable the Keep-Alive header, since that feature is not exposed in any of the built-in binding classes.

The easiest way to achieve this without having to define a custom binding from scratch, is to customize the existing BasicHttpBinding or WSHttpBinding instance associated to the client proxy in code.

Here's an example:

var proxy = new MyServiceClient();
var customBinding = new CustomBinding(proxy.Endpoint.Binding);
var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
transportElement.KeepAliveEnabled = false;

proxy.Endpoint.Binding = customBinding;
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • 1
    In this case same configuration doesn't have to be applied on the server side. Keep alive is HTTP transport feature and it is up to every participant how it will be configured. – Ladislav Mrnka Jun 30 '11 at 13:42
  • @Ladislav Mrnka Absolutely correct. This setting *could*, however, be applied on the server. I updated my answer. – Enrico Campidoglio Jun 30 '11 at 13:51
  • Many thanks. I didn't realise keepalives were enabled by default, so that's one less thing for me to do :) – Nathan Jun 30 '11 at 17:00
5

Keep alive is enabled by default on all HTTP based bindings and it is not possible to turn it off. If you want to turn it off you must create whole new custom binding and set keepAliveEnabled to false on httpTransport binding element.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 6
    To be clear, it's not possible to turn off keep-alive on the built-in http based bindings (basicHttpBinding, wsHttpBinding, etc..) because these bindings do not expose the property. It can be turned off in custom bindings. I know that's what you said, but the way you phrased it was confusing and I wanted to clarify. – Erik Funkenbusch Dec 21 '14 at 10:23