簡體   English   中英

.NET ServicePoint 對象生命周期

[英].NET ServicePoint object lifecycle

當我的 Web API 啟動時,我一直在設置ServicePoint.ConnectionLeaseTimeout屬性,以便使用ServicePointManager.FindServicePoint定期刷新連接以實現負載平衡方案。 但是,我發現在為 ServicePoint 使用 HTTP 連接后的某個時間, ServicePoint.ConnectionLeaseTimeout已從我的設置值更改為默認值 -1。 我絕對不會在我的代碼中的任何地方將ServicePoint.ConnectionLeaseTimeout設置為 -1,而我配置的其他服務點(尚未用於任何連接)仍處於配置值。 因此,我必須假設我最初配置的 ServicePoint 已被處理並替換為一個新的 ServicePoint 對象,其中ServicePoint.ConnectionLeaseTimeout屬性具有默認值。

我知道當超過ServicePointManager.MaxServicePoints時可以刪除 ServicePoints,但在我的應用程序中ServicePointManager.MaxServicePoints設置為 0(無限制)。 我也知道 ServicePoint 可能會在超過ServicePointManager.MaxServicePointIdleTime后被處理,但我將它設置為 int.MaxValue (~25 天)並且那個時間肯定沒有過去。

有沒有人知道如何管理 ServicePoint 的生命周期以及如何更好地管理 ServicePoint 的配置(特別是 ConnectionLeaseTimeout),而無需在應用程序啟動時簡單地找到 ServicePoint 並設置一次?

ServicePointManager 的源代碼顯示 ServicePoint 對象包裝在WeakReference對象中並存儲在Hashtable中。 WeakReference 類型提供對對象的引用,同時仍允許該對象被垃圾回收回收。 因此,垃圾收集器可能會處理掉 ServicePoint,而 ServicePointManager 隨后重新生成了一個新的 ServicePoint 對象來替換它,該對象不會保留 ServicePointManager 無法控制的先前配置值。 似乎沒有辦法靜態配置 ConnectionLeaseTimeout 以便將其應用於新創建的 ServicePoint 對象。

ServicePointManager 的源代碼顯示,當連接空閑時間超過ServicePointManager.MaxServicePointIdleTime值時,TimerThreads 用於刪除 ServicePoint 對象。 默認值為 100 秒。 每個 ServicePoint 將使用創建 ServicePoint 時設置的空閑時間(對 TimerThread.Queue 的引用傳遞到 ServicePoint 構造函數),因此更新 ServicePointManager.MaxServicePointIdleTime 不會調整任何現有 ServicePoint 對象的 MaxIdleTime,因此確保在使用任何 HTTP 連接之前配置它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM