HttpClient & HttpMessageHandler & HttpMessageHandler

Make HTTP requests using IHttpClientFactory in ASP.NET Core | Microsoft Docs

 

HttpClient and lifetime management

A new HttpClient instance is returned each time CreateClient is called on the IHttpClientFactory.

An HttpMessageHandler is created per named client. The factory manages the lifetimes of the HttpMessageHandler instances.

 

IHttpClientFactory pools the HttpMessageHandler instances created by the factory to reduce resource consumption. An HttpMessageHandler instance may be reused from the pool when creating a new HttpClient instance if its lifetime hasn't expired.

 

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 (Domain Name System) changes.

 

The default handler lifetime is two minutes. The default value can be overridden on a per named client basis:

builder.Services.AddHttpClient("HandlerLifetime")
    .SetHandlerLifetime(TimeSpan.FromMinutes(5));

  

HttpClient instances can generally be treated as .NET objects not requiring disposal.

Disposal cancels outgoing requests and guarantees the given HttpClient instance can't be used after calling Dispose

IHttpClientFactory tracks and disposes resources used by HttpClient instances.

 

Keeping a single HttpClient instance alive for a long duration is a common pattern used before the inception of IHttpClientFactory. This pattern becomes unnecessary after migrating to IHttpClientFactory.

 

Alternatives to IHttpClientFactory

Using IHttpClientFactory in a DI-enabled app avoids:

  • Resource exhaustion problems by pooling HttpMessageHandler instances.
  • Stale DNS problems by cycling HttpMessageHandler instances at regular intervals.

There are alternative ways to solve the preceding problems using a long-lived SocketsHttpHandler instance.

  • Create an instance of SocketsHttpHandler when the app starts and use it for the life of the app.
  • Configure PooledConnectionLifetime to an appropriate value based on DNS refresh times.
  • Create HttpClient instances using new HttpClient(handler, disposeHandler: false) as needed.

The preceding approaches solve the resource management problems that IHttpClientFactory solves in a similar way.

  • The SocketsHttpHandler shares connections across HttpClient instances. This sharing prevents socket exhaustion.
  • The SocketsHttpHandler cycles connections according to PooledConnectionLifetime to avoid stale DNS problems.

 

posted @ 2022-07-11 17:59  PanPan003  阅读(115)  评论(0编辑  收藏  举报