Articles
C# Tip: use IHttpClientFactory to generate HttpClient instances
The problem with HttpClient When you create lots of HttpClient instances, you may incur Socket Exhaustion. This happens because sockets are a finite resource, and they are not released exactly when you 'Dispose' them, but a bit later. So, when you create lots of clients, you may terminate the available sockets.
he purpose of IHttpClientFactory
is to solve that issue with HttpMessageHandler
.
An interesting feature of IHttpClientFactory
is that you can customize it with some general configurations that will be applied to all the HttpClient
instances generated in a certain way. For instance, you can define HTTP Headers, Base URL, and other properties in a single point, and have those properties applied everywhere.