HttpClientFactoryServiceCollectionExtensions.AddHttpClient has several overloads. Some of them has AddHttpClient<TClient>
signature and other has AddHttpClient<TClient,TImplementation>
.
According to the Microsoft documentation:
AddHttpClient<TClient,TImplementation>
- Adds the IHttpClientFactory and related services to the IServiceCollection and configures a binding between the TClient type and a named HttpClient.
AddHttpClient<TClient>
- Adds the IHttpClientFactory and related services to the IServiceCollection and configures a binding between the TClient type and a named HttpClient.
I dont' understand when I supposed to use the first overload and when I supposed to use the second overload.
The difference is that the second one will register TImplementation
as TClient
, i.e. if you have an interface like IMyTypedClient
you want to resolve then you will use the first one:
builder.Services.AddHttpClient<IMyTypedClient, MyTypedClientImpl>();
And then you will resolve IMyTypedClient
in some service.
And if you want to resolve the implementation directly then you will use the first one - builder.Services.AddHttpClient<MyTypedClientImpl>();
and then you will need to resolve MyTypedClientImpl
.
Basically the relation is the same as between Add{Lifetime}<TService>
and Add{Lifetime}<TService,TImplementation>
methods.