以编程方式为WebHttpBinding设置keep-alive

时间:2011-10-27 21:34:13

标签: c#-4.0

我想知道如何为webHttpBinding禁用keepAlive。我知道我可以这样做:

<bindings>
<customBinding>
    <binding name="WebHttpWithoutKeepAlive">
        <webMessageEncoding />
        <httpTransport keepAliveEnabled="false" />
    </binding>
</customBinding>
</bindings>
<services>
<service name="MyService" behaviorConfiguration="myServiceBehavior">
<endpoint address="http://localhost:9005/"
      binding="customBinding"
      bindingConfiguration="WebHttpWithoutKeepAlive"
      contract="IMyService"
      behaviorConfiguration="myServerEndpointBehavior"/>
</service>
</services>

我如何以编程方式执行相同的操作?

1 个答案:

答案 0 :(得分:4)

private Binding CreateBinding()
{
    Binding binding = new WebHttpBinding();

    CustomBinding customBinding = new CustomBinding(binding);
    var transportElement = customBinding.Elements.Find<HttpTransportBindingElement>();
    transportElement.KeepAliveEnabled = false;

    return customBinding;
}