ASP.NET Core Web API Interview Question #161

What is IHttpClientFactory?

Testing, HttpClient, Integration Testing, Background Services & Deployment Senior Advanced

Quick Interview Answer

IHttpClientFactory creates configured HttpClient instances while managing the lifetime of underlying message handlers. It integrates HTTP clients with dependency injection, logging, configuration, and resilience features.

Detailed Explanation

IHttpClientFactory creates configured HttpClient instances while managing the lifetime of underlying message handlers. It integrates HTTP clients with dependency injection, logging, configuration, and resilience features.

It avoids common problems caused by repeatedly constructing and disposing HttpClient instances and provides a central place to configure outbound dependencies.

Code Example

builder.Services.AddHttpClient("catalog", client =>
{
    client.BaseAddress = new Uri("https://catalog.example.com/");
    client.Timeout = TimeSpan.FromSeconds(10);
});