HttpClientFactoryOptions.HandlerLifetime Property

Definition

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. Set the lifetime to InfiniteTimeSpan to disable handler expiry.

public:
 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

Property Value

Remarks

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.

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. Some handlers also keep connections open indefinitely which can prevent the handler from reacting to DNS changes. 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. Using long-lived HttpClient instances will prevent the underlying HttpMessageHandler from being disposed until all references are garbage-collected.

Applies to