2011-10-27 95 views
2

我在想如何禁用webHttpBinding的keepAlive。我知道我可以這樣做:以編程方式爲WebHttpBinding設置保持活動狀態

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

我該如何以編程方式做同樣的事情?

回答

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

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

    return customBinding; 
}