【问题标题】:How to troubleshoot C# IIS-hosted WCF NET TCP service timeout issues如何解决 C# IIS 托管的 WCF NET TCP 服务超时问题
【发布时间】:2020-09-14 08:32:25
【问题描述】:

我在 Windows Server 2019 上继承了由 IIS 托管的 C# Web 服务,该服务遇到了我正在努力追查的问题。

WCF 服务在端口 82 上侦听 http 连接,在端口 8002 上侦听 net tcp 连接;该站点的 IIS 配置将其绑定设置为 http:*:82:,net.tcp:8002:*

但是,当服务运行几个小时后,它会停止在 net 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 连接再次开始正常工作,这对我来说可能是某种资源泄漏,但我在代码中看不到任何明显的东西,而且似乎没有是内存不足。

我不知道如何进一步解决这个问题 - 有什么建议吗?

【问题讨论】:

  • 永远不要使用低于 1000 的端口号,它们是为标准协议保留的,并且经常被病毒检查程序阻止。 Sop 将端口号从 82 更改为不同的数字。要从 cmd.exe 调试问题,请使用 >Netstat -a(在客户端和服务器上运行),它将给出每个端口的连接状态。服务器必须正在侦听端口。从客户端到服务器的连接也必须完全关闭,才能从相同的客户端 IP 建立新连接。通常这样的情况是由于旧连接永远不会关闭。
  • docs.microsoft.com/en-us/dotnet/framework/wcf/diagnostics/… 您可能会从跟踪中获得一些初步想法,但挂起转储分析或分析器可能会更快(但需要技能)。

标签: c# wcf iis network-programming


【解决方案1】:

根据你的描述,问题可能是连接的客户端数量超过了服务器端的最大容量,服务器处理不了。如果客户端等待超时,就会出现TimeoutException。

您可以通过以下代码查看该服务的最大连接数:

        NetTcpBinding binding = new NetTcpBinding();
        Console.WriteLine(binding.MaxConnections);

这是maxconnections的参考链接:

https://docs.microsoft.com/en-us/dotnet/api/system.servicemodel.nettcpbinding.maxconnections?view=netframework-4.8

WCF服务中有servicethrottling属性,指定windows通信基础(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多