我继承了一个由Windows Server2019上的IIS托管的C# web服务,它有一个我正在努力追踪的问题。
WCF服务在端口82上侦听http连接,在端口8002上侦听net tcp连接;站点的IIS配置将其绑定设置为http:*:82:,net.tcp:8002:*
但是,当该服务运行了几个小时后,它会停止在网络tcp端口(8002)上响应-尝试连接到它的其他应用程序最终会超时:
System.TimeoutException: The open operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout. ---> System.TimeoutException: The socket transfer timed out after 00:01:00. You have exceeded the timeout set on your binding. The time allotted to this operation may have been a portion of a longer timeout. ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
然而,到http端口(82)的连接仍按预期工作,因此该服务仍在运行。
回收适当的应用程序池会导致net tcp连接再次开始正常工作,这表明这可能是某种资源泄漏,但我在代码中看不到任何明显的东西,而且看起来也不是内存不足。
我对如何进一步解决这个问题感到困惑-有什么建议吗?
发布于 2020-05-28 14:53:45
根据您的描述,问题可能是连接的客户端数量超过了服务器端的最大容量,服务器无法处理。如果客户端等待超时,则会出现TimeoutException。
您可以通过以下代码查看服务的最大连接数:
NetTcpBinding binding = new NetTcpBinding();
Console.WriteLine(binding.MaxConnections);这是maxconnections的参考链接:
WCF服务中存在服务轮询属性,它规定了WCF服务的约束机制。您可以通过在服务中设置serviceThrottling来防止资源的过度消耗。
以下配置示例指定服务最大并发调用数限制为2,最大并发实例数限制为10。

为了防止过度消耗资源,客户端每次调用后都需要关闭通道。
ServiceReference1.Service1Client service1Client = new ServiceReference1.Service1Client();
service1Client.GetData(0);
service1Client.Close();有关serviceThrottling的更多信息,请参阅以下链接:
https://docs.microsoft.com/zh-cn/dotnet/framework/configure-apps/file-schema/wcf/servicethrottling
https://stackoverflow.com/questions/62040772
复制相似问题