0

ServicePoint.ConnectionLeaseTimeout当我的 Web API 启动时,我一直在设置该属性,以便使用ServicePointManager.FindServicePoint. 但是,我发现在为 ServicePoint 使用 HTTP 连接后的某个时间,ServicePoint.ConnectionLeaseTimeout它已从我的设置值更改为默认值 -1。我绝对没有ServicePoint.ConnectionLeaseTimeout在我的代码中的任何地方将 -1 设置为 -1,而我配置的其他服务点(尚未用于任何连接)仍处于配置值。因此,我必须假设我最初配置的 ServicePoint 已被处置并替换为ServicePoint.ConnectionLeaseTimeout属性具有默认值的新 ServicePoint 对象。

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

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

4

2 回答 2

1

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

于 2018-12-10T15:48:52.230 回答
0

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

于 2018-12-10T17:32:31.693 回答