HCR088¶
A typed client registered via AddHttpClient<T> must have a constructor that accepts HttpClient.
Why¶
AddHttpClient<T> registers T with a factory that creates the configured HttpClient and passes it to the typed client's constructor. When T has no constructor parameter of type HttpClient, the runtime factory cannot inject the client — the registration silently resolves a typed client that never receives the configured HttpClient, or fails at resolution time depending on the Microsoft.Extensions.DependencyInjection container.
Bad¶
public sealed class GitHubClient
{
public GitHubClient(IConfiguration configuration) { }
}
services.AddHttpClient<GitHubClient>();
Better¶
Accept the HttpClient the factory provides:
public sealed class GitHubClient
{
private readonly HttpClient _httpClient;
public GitHubClient(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
}
}
Current Detection¶
The implementation reports AddHttpClient<T> and AddHttpClient<TClient, TImplementation> invocations on Microsoft.Extensions.DependencyInjection receivers when the constructed type has no instance constructor with an HttpClient parameter. Only true factory delegates are skipped: a delegate argument whose return type is not void (for example Func<HttpClient>, Func<IServiceProvider, HttpClient>, or Func<HttpClient, TImplementation>) supplies the instance itself. Configure delegates such as Action<HttpClient> still rely on the typed client constructor and are analyzed. Abstract types, interfaces, unresolved type arguments, and non-framework AddHttpClient lookalikes are skipped.
Suppression¶
Suppress only when the typed client intentionally obtains its HttpClient through another mechanism (for example, an IHttpClientFactory parameter it calls itself). Prefer adding the HttpClient parameter — that is the pattern the registration API is built around.