HTTP Keep-alive and ServiceHost / C#?
How can I enable/disable the HTTP Keep-alive and set the connection timeout of a self hosted service using the application configuration and the 开发者_开发技巧C# ServiceHost?
For example,
MyService service = new MyService();
ServiceHost serviceHost = new ServiceHost(service);
serviceHost.Open();
What do I have to put in the application configuration to set the http keep alive and timeout.
<configuration>
<system.serviceModel>
<services>
<service name="MyService" behaviorConfiguration="myServiceBehavior">
<endpoint address="http://localhost:9005/"
binding="webHttpBinding"
contract="IMyService"
behaviorConfiguration="myServerEndpointBehavior"/>
</service>
</services>
</system.serviceModel>
<!-- WHERE TO ENABLE/DISABLE http keep alive and timeout -->
</configuration>
The settings under IIS are found if you go to the IIS Manager. Right click on the "Default Web Site"->Properties->Web Site->Connections. Can I do that through the system.serviceModel configuration?
Keep alive for http connections is turned on by default. If you want to turn it off you must create custom binding:
<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>
How to set a keep-alive timout is a mystery. There is no "service timeout" in your scenario (it makes sense only in bindings with session) and keep-alive timeout doesn't abort communication between client and service.
Btw. your example code defines service as singleton object.
精彩评论