WCF& keepAliveEnabled - 同时有两个以上的套接字?

时间:2014-09-11 16:19:47

标签: c# multithreading wcf keep-alive

我尝试创建一个多线程请求者,以测试我的服务器的回答时间。

我的服务器是WCF Web服务,我的客户端有对此Web服务的服务引用。

WCF尊重HTTP规范,我在许多网站上看到WCF默认启用了Keep-Alive。只有来自同一客户端的两个连接同时被授权。

因此,当我尝试创建100个线程时,只有两个线程同时处理。因此,经过几次循环后,很多线程都处于超时状态。

我尝试按照这篇文章:WCF wsHttpBinding with http keepalive但它没有用。

这是我的应用配置(由服务参考修改):

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ISabIn" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://xxx.xxx.xxx.xxx:xxx/SabIn.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ISabIn" contract="TestWs.ISabIn"
                name="BasicHttpBinding_ISabIn" />
        </client>
    </system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

我尝试在appCode中使用customBinding,就像这篇文章:HTTP Keep-alive and ServiceHost / C#? 但是,当我执行我的软件时,会出现关于“application / xml”的错误。如果我尝试用英语翻译,那就像是“你的客户端不管理应用程序/ xml消息”。

我需要做实时交易,如果WCF只能同时管理两个套接字我就不能...

你能帮助我吗?

PS:我可以发布我的客户端的代码,但我不确定是否有用,因为我知道问题是套接字限制。

SOLUTION(thnaks to Spender):在代码的开头,在System.Net.ServicePointManager.DefaultConnectionLimit中更改默认值(它是2)

1 个答案:

答案 0 :(得分:2)

这是否是由对单个主机的HTTP请求的默认.net限制造成的?这是因为ServicePointManager.DefaultConnectionLimit默认为2。

在内部发出HTTP请求时,会创建ServicePoint实例来管理与特定主机的连接。向同一主机发出新请求,它将使用相同的ServicePoint实例进行连接管理。

您可以使用以下代码操作单个主机的限制:

var serviceUri = new Uri("http://xxx.xxx.xxx.xxx:xxx/SabIn.svc");
var servicePoint = System.Net.ServicePointManager.FindServicePoint(serviceUri);
servicePoint.ConnectionLimit = int.MaxValue;

我相信您也可以handle this in web.config/app.config,但我不确定您是否可以调整与单个ServicePoint相关的限制。