简体   繁体   English

.NET ServicePoint 对象生命周期

[英].NET ServicePoint object lifecycle

I have been setting the ServicePoint.ConnectionLeaseTimeout property when my web API starts up so that connections are refreshed regularly for load balancing scenarios by using ServicePointManager.FindServicePoint .当我的 Web API 启动时,我一直在设置ServicePoint.ConnectionLeaseTimeout属性,以便使用ServicePointManager.FindServicePoint定期刷新连接以实现负载平衡方案。 But, I am finding that at some time after using HTTP connections for the ServicePoint the ServicePoint.ConnectionLeaseTimeout has changed from my set value to the default -1.但是,我发现在为 ServicePoint 使用 HTTP 连接后的某个时间, ServicePoint.ConnectionLeaseTimeout已从我的设置值更改为默认值 -1。 I am definitely not setting the ServicePoint.ConnectionLeaseTimeout to -1 anywhere in my code and the other service points that I have configured, which have not been used for any connections are still at the configured value.我绝对不会在我的代码中的任何地方将ServicePoint.ConnectionLeaseTimeout设置为 -1,而我配置的其他服务点(尚未用于任何连接)仍处于配置值。 So, I must assume that the ServicePoint that I originally configured has been disposed of and replaced with a new ServicePoint object where the ServicePoint.ConnectionLeaseTimeout property has the default value.因此,我必须假设我最初配置的 ServicePoint 已被处理并替换为一个新的 ServicePoint 对象,其中ServicePoint.ConnectionLeaseTimeout属性具有默认值。

I know that ServicePoints can be deleted when ServicePointManager.MaxServicePoints is exceeded, but in my application ServicePointManager.MaxServicePoints is set to 0 (unlimited).我知道当超过ServicePointManager.MaxServicePoints时可以删除 ServicePoints,但在我的应用程序中ServicePointManager.MaxServicePoints设置为 0(无限制)。 I'm also aware that the ServicePoint may be disposed after ServicePointManager.MaxServicePointIdleTime is exceeded, but I am setting it to int.MaxValue (~25 days) and that time has definitely not passed.我也知道 ServicePoint 可能会在超过ServicePointManager.MaxServicePointIdleTime后被处理,但我将它设置为 int.MaxValue (~25 天)并且那个时间肯定没有过去。

Does anyone have any other information on how the lifecycle of ServicePoint is managed and how to better manage the configuration of a ServicePoint (in particular ConnectionLeaseTimeout) without simply finding the ServicePoint at application start up and setting it once?有没有人知道如何管理 ServicePoint 的生命周期以及如何更好地管理 ServicePoint 的配置(特别是 ConnectionLeaseTimeout),而无需在应用程序启动时简单地找到 ServicePoint 并设置一次?

The source code for ServicePointManager shows that the ServicePoint objects are wrapped inside WeakReference objects and stored in a Hashtable . ServicePointManager 的源代码显示 ServicePoint 对象包装在WeakReference对象中并存储在Hashtable中。 The WeakReference type provides a reference to an object while still allowing that object to be reclaimed by garbage collection. WeakReference 类型提供对对象的引用,同时仍允许该对象被垃圾回收回收。 So it might be the case that the garbage collector has disposed of the ServicePoint and the ServicePointManager has subsequently regenerated a new ServicePoint object to replace it, which would not hold the previous configuration values that the ServicePointManager does not control.因此,垃圾收集器可能会处理掉 ServicePoint,而 ServicePointManager 随后重新生成了一个新的 ServicePoint 对象来替换它,该对象不会保留 ServicePointManager 无法控制的先前配置值。 There doesn't appear to be a way to configure ConnectionLeaseTimeout statically so that it will be applied to newly created ServicePoint objects.似乎没有办法静态配置 ConnectionLeaseTimeout 以便将其应用于新创建的 ServicePoint 对象。

The source code for ServicePointManager shows that TimerThreads are used to remove ServicePoint objects when the connections have been idle for longer than the ServicePointManager.MaxServicePointIdleTime value. ServicePointManager 的源代码显示,当连接空闲时间超过ServicePointManager.MaxServicePointIdleTime值时,TimerThreads 用于删除 ServicePoint 对象。 The default is 100 seconds.默认值为 100 秒。 Each ServicePoint will use the idle time as it is set when the ServicePoint is created (a reference to the TimerThread.Queue is passed into the ServicePoint constructor), so updating the ServicePointManager.MaxServicePointIdleTime will not adjust the MaxIdleTime of any existing ServicePoint objects, so make sure to configure this before using any HTTP connections.每个 ServicePoint 将使用创建 ServicePoint 时设置的空闲时间(对 TimerThread.Queue 的引用传递到 ServicePoint 构造函数),因此更新 ServicePointManager.MaxServicePointIdleTime 不会调整任何现有 ServicePoint 对象的 MaxIdleTime,因此确保在使用任何 HTTP 连接之前配置它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM