ASP.NET Core Interview Question #192
What are named and typed HttpClient clients?
Performance, Caching & HTTP Senior Advanced
Quick Interview Answer
A named client is configured under a string name and created through IHttpClientFactory. It is useful when several logical clients need different base addresses, headers, or settings.
Detailed Explanation
A named client is configured under a string name and created through IHttpClientFactory. It is useful when several logical clients need different base addresses, headers, or settings.
A typed client wraps HttpClient in a strongly typed service class. It provides a clear API for one external service and allows the client to receive additional dependencies through DI.
Typed clients often make application code easier to test and keep external-service details out of controllers.
Code Example
builder.Services.AddHttpClient<GitHubClient>(client =>
{
client.BaseAddress = new Uri("https://api.github.com/");
client.DefaultRequestHeaders.UserAgent.ParseAdd("MyApp");
});
public class GitHubClient
{
private readonly HttpClient _http;
public GitHubClient(HttpClient http) => _http = http;
}