首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.NET ServicePoint对象生命周期

.NET ServicePoint对象生命周期
EN

Stack Overflow用户
提问于 2018-12-06 14:31:47
回答 2查看 889关注 0票数 0

在我的web启动时,我一直在设置ServicePoint.ConnectionLeaseTimeout属性,以便通过使用ServicePointManager.FindServicePoint定期刷新连接以进行负载平衡场景。但是,我发现在为ServicePoint使用HTTP之后,ServicePoint.ConnectionLeaseTimeout已经从我的设置值更改为默认的-1。我肯定不会在代码中的任何地方将ServicePoint.ConnectionLeaseTimeout设置为-1,而我所配置的其他服务点(未用于任何连接)仍处于配置值。因此,我必须假定我最初配置的ServicePoint已被释放并替换为一个新的ServicePoint对象,其中ServicePoint.ConnectionLeaseTimeout属性具有默认值。

我知道在超过ServicePoints时可以删除ServicePointManager.MaxServicePoints,但是在我的应用程序中,ServicePointManager.MaxServicePoints设置为0(无限)。我还知道,ServicePoint可能会在超过ServicePointManager.MaxServicePointIdleTime之后被释放,但我将其设置为int.MaxValue (~25天),而且时间肯定没有过去。

有没有人知道ServicePoint的生命周期是如何管理的,以及如何更好地管理ServicePoint的配置(特别是ConnectionLeaseTimeout),而不只是在应用程序启动和设置一次的时候找到ServicePoint?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-10 15:48:52

ServicePointManager的源代码显示,ServicePoint对象包装在WeakReference对象中,并存储在哈希表中。WeakReference类型提供了对对象的引用,同时仍然允许垃圾回收回收该对象。因此,垃圾收集器已经释放了ServicePoint,而ServicePointManager随后重新生成了一个新的ServicePoint对象来替换它,这将不会保存ServicePointManager不控制的以前的配置值。似乎没有一种方法可以静态地配置ConnectionLeaseTimeout,以便将其应用于新创建的ServicePoint对象。

票数 1
EN

Stack Overflow用户

发布于 2018-12-10 17:32:31

ServicePointManager的源代码显示,当连接空闲时间超过ServicePointManager.MaxServicePointIdleTime值时,TimerThreads用于删除ServicePoint对象。默认为100秒。每个ServicePoint将使用在创建ServicePoint时设置的空闲时间(对TimerThread.Queue的引用被传递到ServicePoint构造函数),因此更新ServicePointManager.MaxServicePointIdleTime不会调整任何现有ServicePoint对象的MaxIdleTime,因此在使用任何HTTP之前请确保对其进行配置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53653640

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档