When you call any of the AddHttpClient extension methods, you're adding the IHttpClientFactory and related services to the IServiceCollection.
Manages the caching and lifetime of underlying HttpClientHandler instances. Automatic management avoids common Domain Name System (DNS) problems that occur when manually managing HttpClient lifetimes.
Each time CreateClient is called:A new instance of HttpClient is created.The configuration action is called.
Provide a single location to configure and interact with a particular HttpClient. For example, a single typed client might be used:
For a single backend endpoint.
To encapsulate all logic dealing with the endpoint.
The typed client is registered as transient with DI.
Using typed clients in singleton services can be dangerous .
Avoid typed clients in singleton services
HttpClient lifetime management
A new HttpClient instance is returned each time CreateClient is called on the IHttpClientFactory. One HttpClientHandler instance is created per client name.The factory manages the lifetimes of the HttpClientHandler instances.
IHttpClientFactory caches the HttpClientHandler instances created by the factory to reduce resource consumption. An HttpClientHandler instance may be reused from the cache when creating a new HttpClient instance if its lifetime hasn't expired.
Caching of handlers is desirable as each handler typically manages its own underlying HTTP connection pool. Creating more handlers than necessary can result in socket exhaustion and connection delays. Some handlers also keep connections open indefinitely, which can prevent the handler from reacting to DNS changes.
The default handler lifetime is two minutes. To override the default value, call SetHandlerLifetime for each client, on the IServiceCollection:
services.AddHttpClient("Named.Client")
.SetHandlerLifetime(TimeSpan.FromMinutes(5));
HttpClient instances created by IHttpClientFactory are intended to be short-lived.
As soon as a typed client instance is created, IHttpClientFactory has no control over it. If a typed client instance is captured in a singleton, it may prevent it from reacting to DNS changes, defeating one of the purposes of IHttpClientFactory.
Recycling and recreating HttpMessageHandler's when their lifetime expires is essential for IHttpClientFactory to ensure the handlers react to DNS changes. HttpClient is tied to a specific handler instance upon its creation, so new HttpClient instances should be requested in a timely manner to ensure the client will get the updated handler.
Disposing of such HttpClient instances created by the factory will not lead to socket exhaustion, as its disposal will not trigger disposal of the HttpMessageHandler. IHttpClientFactory tracks and disposes of resources used to create HttpClient instances, specifically the HttpMessageHandler instances, as soon their lifetime expires and there's no HttpClient using them anymore.
reference : https://learn.microsoft.com/en-us/dotnet/core/extensions/httpclient-factory
https://www.linkedin.com/posts/farhad-nosrati-%D9%81%D8%B1%D9%87%D8%A7%D8%AF-%D9%86%D8%B5%D8%B1%D8%AA%DB%8C-231b84185_ihttpclientfactory-when-you-call-any-of-activity-7106673689277911040-Y7CE?utm_source=share&utm_medium=member_desktop