如何使用应用程序配置和C# ServiceHost启用/禁用HTTP Keep-alive以及设置自托管服务的连接超时?
例如,
MyService service = new MyService();
ServiceHost serviceHost = new ServiceHost(service);
serviceHost.Open();我必须在应用程序配置中放入什么才能设置http保持活动和超时。
<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>如果您转到IIS管理器,则可以找到IIS下的设置。右键单击“默认网站”->“属性”->“网站”->“连接”。我能通过system.serviceModel配置做到这一点吗?
发布于 2011-04-15 16:52:25
默认情况下,为http连接保持活动状态处于打开状态。如果你想关闭它,你必须创建自定义绑定:
<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。在您的场景中没有“服务超时”(它只有在绑定会话时才有意义),并且保持活动超时不会中止客户端和服务之间的通信。
顺便说一句。您的示例代码将服务定义为单例对象。
https://stackoverflow.com/questions/5674171
复制相似问题