代码改变世界

WCF 附录 高级主题 配置HTTP连接

2011-06-12 17:21  DanielWise  阅读(3202)  评论(11编辑  收藏  举报
HTTP 1.1 标准中包含了一个叫做HTTP Keep-Alive 的特性。HTTP Keep-Alive 允许HTTP在客户端与服务端之间保持连接。这允许客户端保持连接为打开状态以便于它们可以为顺序请求重用这些连接。为了限制一个单一客户端的资源使用,HTTP 1.1 标准规定每个应用程序连接到每个服务端最多可以保留两条连接。默认情况下,.NET Framework 中的HTTP客户端包括WCF使用HTTP 1.1 Keep-Alives.
  HttpTransportBindingElement绑定元素使用System.Net命名空间中的类来管理并生成HTTP请求。HttpTransportBindingElement绑定元素使用HttpWebRequest类来进行HTTP请求。HttpWebRequest类使用其他类,ServicePointManager 和 ServicePoint类来管理HTTP连接。这些类帮助管理HTTP连接以及它们的生存周期。这部分讨论如何管理HTTP连接以获得WCF服务最好的性能和扩展性。
 
回收空闲连接
ServicePoint类中的MaxIdleTime属性确定连接可以在它们关闭前保持空闲的时间。每个新生成的ServicePoint类实例有一个默认值100秒。这基于ServicePointManager类的MaxServicePointIdleTime属性。当服务在跨越一个场的服务器之间进行负载均衡时调整这个属性是特别重要的。将这个值设置的很低将增加空闲连接被回收的可能性。这允许客户端在负载均衡服务场中建立于其他服务的新连接。列表A.6显示了如何在ServicePoint类中调整MaxIdleTime属性。
 
列表A.6 使用代码为HTTP设置MaxIdleTime
        public void SetConnections()
        {
            Uri myUri = new Uri("http://www.somewhere.com/");
            ServicePoint sp = ServicePointManager.FindServicePoint(myUri);
            sp.MaxIdleTime = 30000;
        }
 
整连接生存周期
ServicePoin类中的ConnectionLeaseTimeout属性确定一个连接在它可以被回收之前最大活跃时间。每个ServicePoint实例有一个-1的默认值。 –1 意味着连接可以永远保持打开。 这对负载均衡环境可能不是很必要,因为客户端仍然连接到同样的服务端。如果ConnectionLeaseTimeout设置的值大于0那么连接可以在一段时间后被回收。如果连接生命周期完成,活跃连接会关闭同时会创建一个新的连接。在每次请求后可以通过设置ConnectionLeaseTimeout为0来强制关闭连接。列表A.7 显示如何在ServicePoint类中调整ConnectionLeaseTimeout值。
 
列表A.7 使用代码为HTTP设置ConnectionLeaseTimeout
        public void SetConnections()
        {
            Uri myUri = new Uri("http://www.somewhere.com/");
            ServicePoint sp = ServicePointManager.FindServicePoint(myUri);
            sp.ConnectionLeaseTimeout = 30;
        }
 
禁用HTTP Keep-Alives
调整ServicePoint类的MaxIdleTime和ConnectionLeaseTimeout属性帮助管理连接生命周期。当在负载均衡服务场环境中时这尤其重要。不幸的是,并不是所有负载均衡场景支持HTTP Keep-Alives. 有时候实现负载均衡的唯一方式是关闭使用HTTP Keep-Alives. 这可以通过很多种方式实现。
  很多位置影响是否使用HTTP Keep-Alives. 例如,HTTP Keep-Alives 可以通过因特网信息服务设置(IIS)。图片A.2 显示了Windows Server 2008 和Windows Vista SP1 中IIS 7.0 里的HTTP回复消息头特性。
  IIS 7.0 设置HTTP Keep-Alives也可以使用命令行。下面的命令行显示了如何为IIS 7.0 禁用HTTP Keep-Alives。 这是在Windows Vista 之前版本到SP1 版本在IIS 7.0 中禁用HTTP Keep-Alives的唯一方式。
  appcmd set config /section:httpProtocol /allowKeepAlive:false
HTTP Keep-Alives 也可以使用HttpTransportBindingElement绑定元素的KeepAliveEnabled 属性来使用或者禁用。列表A.8 显示了如何在HttpTransportBindingElement绑定元素上禁用HTTP Keep-Alives。
列表A.8 HttpTransportBindingElement绑定元素的HTTP Keep-Alives
        public void SetHttpKeepAlive()
        {
            HttpTransportBindingElement be = new HttpTransportBindingElement();
            be.KeepAliveEnabled = false;
        }
 
图片A.2 IIS 7.0 设置HTTP Keep-Alives
 
  HTTP Keep-Alives 也可以使用一个基于HttpTranportBindingElement绑定元素的自定义绑定来在配置文件中禁用。列表A.9 显示了如何使用配置文件在一个自定义绑定中禁用HTTP Keep-Alives。
列表A.9 使用httpTranport元素设置keepAliveEnabled
 
  最后一个解决方案,先前已经提到过了,在ServicePoint类上设置ConnectinLeaseTimeout。将这个值设置为0将使连接在每次请求之后强制关闭。强制每个新的请求获取一个新的连接。这与禁用HTTP Keep-Alives有同样的效果。
增加连接数量
ServicePoint类中的ConnectionLimit属性确定ServicePoint实例可以打开的最大连接数量。默认值基于宿主环境设置。在客户端的每个ServicePoint实例设置2个连接,在ASP.NET 服务端环境为每个ServicePoint实例设置10个连接。增加ConnectionLimit属性可能导致服务端到服务端的吞吐量增加或者客户端以多线程执行。列表A.10显示了如何使用配置文件设置连接的最大数量。
列表A.10 使用配置文件设置HTTP MaxConnection
  <system.net>
    <connectionManagement>
      <add address="http://www.somewhere.com" maxconnection="1000"/>
    </connectionManagement>
  </system.net>