I think there's a bit of misunderstanding on how to use IHttpClientFactory.
IHttpClientFactory always create HttpClient and never create TClient which is specified as the type argument of AddHttpClient method.
The TClient class is where you can inject the configured client. The Microsoft's documentation says,
A typed client accepts an HttpClient parameter in its constructor
So, configuring services like:
builder.Services.AddHttpClient<CustomHttpClient>(client =>
{
client.BaseAddress = new Uri("http://custom.api.com");
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
builder.Services.AddHttpClient<AnotherHttpClient>(client =>
{
client.BaseAddress = new Uri("http://another.api.com");
}).AddHttpMessageHandler<BaseAddressAuthorizationMessageHandler>();
then, we can implement the CustomHttpClient class like:
public class CustomHttpClient
{
private readonly HttpClient _client;
public CustomHttpClient(HttpClient client)
{
_client = client; //<-- This client has base URL "http://custom.api.com"
}
public Task<string> GetSomething(string apiPath)
{
return _client.GetStringAsync(apiPath);
}
}
If you still want to create HttpClient by name, you can give a name when calling AddHttpClient method as you did, and inject ITypedHttpClientFactory<CustomHttpClient> to CustomHttpClient and create HttpClient from it with name. But in such case, I don't see the reason to use ITypedHttpClientFactory. maybe, IHttpClientFactory is the choice.