HttpClientFactoryOptions.HandlerLifetime 屬性


取得或設定可以重複使用 HttpMessageHandler 執行個體的時間長度。Gets or sets the length of time that a HttpMessageHandler instance can be reused. 每個具名用戶端都可以設定自己的處理常式存留期值。Each named client can have its own configured handler lifetime value. 此屬性的預設值為兩分鐘。The default value of this property is two minutes. 將存留期設定成 InfiniteTimeSpan 以停用處理常式到期時間。Set the lifetime to InfiniteTimeSpan to disable handler expiry.

 property TimeSpan HandlerLifetime { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan HandlerLifetime { get; set; }
member this.HandlerLifetime : TimeSpan with get, set
Public Property HandlerLifetime As TimeSpan




的預設執行 IHttpClientFactory 會將處理站所建立的實例集區 HttpMessageHandler ,以減少資源耗用量。The default implementation of IHttpClientFactory will pool the HttpMessageHandler instances created by the factory to reduce resource consumption. 這項設定會設定處理常式在排程從集區中移除和處置之前,可進行共用的時間量。This setting configures the amount of time a handler can be pooled before it is scheduled for removal from the pool and disposal.

處理常式的集合是需要的做法,因為每個處理常式通常會管理自己的基礎 HTTP 連線;建立比所需數目更多的處理常式可能會導致連線延遲。Pooling of handlers is desirable as each handler typically manages its own underlying HTTP connections; creating more handlers than necessary can result in connection delays. 某些處理常式也會無限期地保持連接,以防止處理常式回應 DNS 變更。Some handlers also keep connections open indefinitely which can prevent the handler from reacting to DNS changes. HandlerLifetime 應選擇的值,並瞭解應用程式在網路環境中回應變更的需求。The value of HandlerLifetime should be chosen with an understanding of the application's requirement to respond to changes in the network environment.

處理常式的到期日不會立即處置處理常式。Expiry of a handler will not immediately dispose the handler. 過期的處理常式會放在個別的集區中,而且只有在它們變成無法存取時,才會以間隔處理處理常式。An expired handler is placed in a separate pool which is processed at intervals to dispose handlers only when they become unreachable. 使用長時間存留 HttpClient 的實例將會導致 HttpMessageHandler 無法處置基礎,直到所有參考都已進行垃圾收集為止。Using long-lived HttpClient instances will prevent the underlying HttpMessageHandler from being disposed until all references are garbage-collected.