WCF:System.Net.SocketException - 通常每个套接字地址(协议/网络地址/端口)只允许使用一次 [英] WCF: System.Net.SocketException - Only one usage of each socket address (protocol/network address/port) is normally permitted

查看:51
本文介绍了WCF:System.Net.SocketException - 通常每个套接字地址(协议/网络地址/端口)只允许使用一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务和一个 Web 应用程序.Web 应用程序以连续的方式调用此 WCF 服务,也就是轮询.在我们的生产环境中,我很少收到此错误.因为,这是用户在抛出此错误时不知道的内部活动.

I have a WCF service and a Web application. Web application makes calls to this WCF service in a continous manner a.k.a polling. In our production environment, I receive this error very rarely. Since, this is an internal activity users were not aware of when this error is thrown.

无法连接到http://localhost/QAService/Service.svc.TCP 错误代码 10048:只有一种用法每个套接字地址的(协议/网络地址/端口)是通常允许 127.0.0.1:80.--->System.Net.WebException:无法连接到远程服务器--->System.Net.Sockets.SocketException:每个套接字地址仅使用一次(协议/网络地址/端口)是通常允许 127.0.0.1:80

Could not connect to http://localhost/QAService/Service.svc. TCP error code 10048: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted 127.0.0.1:80

我无法在我们的 dev/qa 环境中重现这种行为.我已经确保客户端连接在 try..catch..finally 块中关闭.仍然不明白是什么导致了这个问题..有人知道吗?

I am having trouble in reproducing this behaviour in our dev/qa environment. I have made sure that the client connection is closed in a try..catch..finally block. Still don't understand what is causing this issue .. any one aware of this?

注意:我看过这个SO 问题,但似乎没有回答我的问题,因此不是重复问题.

Note: I've looked at this SO question, but not seems to be answering my problem, so it is not repeated questions.

推荐答案

您正在重载 TCP/IP 堆栈.由于套接字在正常操作下如何关闭,Windows(我认为实际上所有套接字堆栈)对可以快速打开的套接字数量有限制.每当套接字关闭时,它都会进入 TIME_WAIT 状态一段时间(240 秒 IIRC).每次轮询时,都会在默认动态范围之外消耗一个套接字(我认为它大约有 5000 个动态端口,略高于 1024),并且每次轮询结束时,该特定套接字都会进入 TIME_WAIT.如果轮询频率足够高,最终会消耗所有可用端口,从而导致 TCP 错误 10048.

You are overloading the TCP/IP stack. Windows (and I think all socket stacks actually) have a limitation on the number of sockets that can be opened in rapid sequence due to how sockets get closed under normal operation. Whenever a socket is closed, it enters the TIME_WAIT state for a certain time (240 seconds IIRC). Each time you poll, a socket is consumed out of the default dynamic range (I think its about 5000 dynamic ports just above 1024), and each time that poll ends, that particular socket goes into TIME_WAIT. If you poll frequently enough, you will eventually consume all of the available ports, which will result in TCP error 10048.

通常,WCF 试图通过池化连接和类似的东西来避免这个问题.不通过 Internet 的内部服务通常就是这种情况.我不确定是否有任何 wsHttp 绑定支持连接池,但 netTcp 绑定应该支持.我认为命名管道不会遇到这个问题.我不能说 MSMQ 绑定.

Generally, WCF tries to avoid this problem by pooling connections and things like that. This is usually the case with internal services that are not going over the internet. I am not sure if any of the wsHttp bindings support connection pooling, but the netTcp binding should. I would assume named pipes does not run into this problem. I couldn't say for the MSMQ binding.

您可以使用两种解决方案来解决此问题.您可以增加动态端口范围,或减少 TIME_WAIT 的时间.前者可能是更安全的方法,但如果您消耗了大量的套接字(这听起来不像您的场景),减少 TIME_WAIT 是更好的选择(或两者兼而有之.)

There are two solutions you can use to get around this problem. You can either increase the dynamic port range, or reduce the period of TIME_WAIT. The former is probably the safer route, but if you are consuming an extremely high volume of sockets (which doesn't sound like the case for your scenario), reducing TIME_WAIT is a better option (or both together.)

更改动态端口范围

  1. 打开注册表.
  2. 打开密钥 HKLMSystemCurrentControlSetServicesTcpipParameters
  3. 编辑(或创建为 DWORD)MaxUserPort 值.
  4. 将其设置为更高的数字.(即 65534)

更改 TIME_WAIT 延迟

  1. 打开注册表.
  2. 打开密钥 HKLMSystemCurrentControlSetServicesTcpipParameters
  3. 编辑(或创建为 DWORD)TcpTimedWaitDelay.
  4. 将其设置为较低的数字.值以秒为单位.(即 60 表示延迟 1 分钟)

上述解决方案之一应该可以解决您的问题.如果更改端口范围后它仍然存在,我会看到尝试增加轮询的周期,这样它发生的频率就会降低......这会给你更多的余地来解决时间等待延迟.作为最后的手段,我会更改时间等待延迟.

One of the above solutions should fix your problem. If it persists after changing the port range, I would see try increasing the period of your polling so it happens less frequently...that will give you more leeway to work around the time wait delay. I would change the time wait delay as a last resort.

这篇关于WCF:System.Net.SocketException - 通常每个套接字地址(协议/网络地址/端口)只允许使用一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆