C# Tip: use IHttpClientFactory to generate HttpClient instances
Initializing HttpClient instances can expose you to Socket Exhaustion problems. You should use IHttpClientFactory instead

I’m a Principal Backend Developer with more than 10 years of professional experience. I’ve been working with Microsoft platforms and frameworks since 2014.
I’ve worked with many tools and frameworks, such as Azure, MongoDB, Docker, and, of course, .NET and C#. I love learning new things, and I think that the best way to learn is to share: that’s why I started my journey as a content creator and conference speaker.
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.
Even with using statements you may end up with Socket Exhaustion.
class ResourceChecker
{
public async Task<bool> ResourceExists(string url)
{
using (HttpClient client = new HttpClient())
{
var response = await client.GetAsync(url);
return response.IsSuccessStatusCode;
}
}
}
Actually, the real issue lies in the disposal of HttpMessageHandler instances. With simple HttpClient objects, you have no control over them.
Introducing HttpClientFactory
The HttpClientFactory class creates HttpClient instances for you.
class ResourceChecker
{
private IHttpClientFactory _httpClientFactory;
public ResourceChecker(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<bool> ResourceExists(string url)
{
HttpClient client = _httpClientFactory.CreateClient();
var response = await client.GetAsync(url);
return response.IsSuccessStatusCode;
}
}
The 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.
How to add it to .NET Core APIs or Websites
How can you use HttpClientFactory in your .NET projects?
If you have the Startup class, you can simply add an instruction to the ConfigureServices method:
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient();
}
You can find that extension method under the Microsoft.Extensions.DependencyInjection namespace.
Wrapping up
In this article, we've seen why you should not instantiate HttpClients manually, but instead, you should use IHttpClientFactory.
Happy coding!
🐧





