ASP.NET Core Interview Question #191
Why should IHttpClientFactory be used for outgoing HTTP calls?
Performance, Caching & HTTP Senior Advanced
Quick Interview Answer
IHttpClientFactory centralizes HttpClient configuration and manages underlying HttpMessageHandler lifetimes. It helps avoid common connection-management problems caused by repeatedly constructing and disposing HttpClient handlers.
Detailed Explanation
IHttpClientFactory centralizes HttpClient configuration and manages underlying HttpMessageHandler lifetimes. It helps avoid common connection-management problems caused by repeatedly constructing and disposing HttpClient handlers.
It supports named and typed clients, logging integration, handler configuration, and resilience pipelines when configured.
The factory does not remove the need for timeouts, cancellation, retry safety, and careful handling of downstream failures.
Code Example
builder.Services.AddHttpClient<WeatherClient>(client =>
{
client.BaseAddress =
new Uri("https://api.example.com/");
client.Timeout = TimeSpan.FromSeconds(10);
});