How to use HttpClient and IHttpClientFactory in .NET Core
By Tan Lee Published on Jan 09, 2025 361
However, one common pitfall is incorrectly instantiating HttpClient for every request. This can result in socket exhaustion due to improper handling of connections.
Here's an example of the wrong way to use HttpClient
:
public static async Task<string> GetData(string url) { using (var httpClient = new HttpClient()) { using (var response = await httpClient.GetAsync(url)) { string data = await response.Content.ReadAsStringAsync(); return data; } } }
Each time GetData
is called, a new HttpClient
instance is created and disposed of, which can quickly exhaust available sockets, especially in high-throughput applications.
To address this issue, we use an IHttpClientFactory
, which manages the lifecycle of HttpClient
instances for you. This ensures that HTTP connections are reused efficiently.
Register IHttpClientFactory in Startup
First, in your Program.cs
or Startup.cs
(depending on your project structure), you need to register IHttpClientFactory
:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddHttpClient("ApiHttpClient"); var app = builder.Build(); await app.RunAsync();
This creates a named HttpClient
instance available throughout the application. You can register multiple named clients if needed, but for now, "ApiHttpClient" will be sufficient.
Inject IHttpClientFactory Into Your Classes
Now, instead of directly instantiating HttpClient
, you should inject IHttpClientFactory
into your classes.
Here's how your repository or service class might look:
namespace MyApp.Services { public class ApiService { private readonly IHttpClientFactory _httpClientFactory; public ApiService(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } public async Task<string> FetchDataAsync(string endpoint) { var client = _httpClientFactory.CreateClient("ApiHttpClient"); var response = await client.GetAsync(endpoint); if (!response.IsSuccessStatusCode) { throw new Exception($"Error: {response.StatusCode} - {response.ReasonPhrase}"); } return await response.Content.ReadAsStringAsync(); } } }
By calling _httpClientFactory.CreateClient("ApiHttpClient")
, we ensure that the HttpClient
instance is reused across requests. This avoids socket exhaustion and enhances performance by maintaining persistent connections.
- How to Initialize TagHelpers in ASP.NET Core with Shared Data
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core
- The request matched multiple endpoints in ASP.NET Core