HttpWebRequest端口耗尽 [英] HttpWebRequest Port Exhaustion

查看:189
本文介绍了HttpWebRequest端口耗尽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Windows服务对内部REST服务进行非常频繁的HTTP调用(每秒20-40个调用),但是请注意,服务运行几分钟后,获取响应的时间过长.

We are using a Windows Service to make very frequent HTTP calls to an internal REST service (20-40 calls per second), but notice a long delay in getting responses after the service runs for several minutes.

看一下netstat,有很多端口处于"TIME_WAIT"状态,看来我们可能已经用完了端口.

Looking at netstat, there are quite a few ports with "TIME_WAIT" status, and it seems we may be running out of ports.

我们如何确保端口被重用?

How can we ensure that ports are reused?

推荐答案

它可能与许多用户在使用HttpClient类和内部使用HttpClient的类时遇到的同一问题有关. HttpClient实现IDisposable接口的结果是,您会发现许多开发人员都渴望将类的任何新实例包装在using语句中.那里只有一个问题.当HttpClient对象被处置后,附加的端口将在TIME_WAIT中停留5分钟,直到它们被操作系统释放为止.

It's probably related to the same issue that many users run into when using the HttpClient class and classes that use the HttpClient internally. As a result of the HttpClient implementing the IDisposable interface, you'll find many developers having the urge to wrap any new instances of the class in a using statement. There is just one problem with that; When the HttpClient object gets disposed of the attached ports remain blocked for 5 minutes 'TIME_WAIT' until they get released by the OS.

我通常使用单个HttpClient实例(单例),并将完全限定的URL与异步调用结合使用.

I usually use a single HttpClient instance (singleton) and use fully qualified URLs in combination with asynchronous calls.

这篇关于HttpWebRequest端口耗尽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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