I'm using .NET Framework 4.7.2 and am trying to use IHttpClientFactory to create HttpClient instances.
I downloaded the Microsoft.Extensions.Http v7 NuGet Package and now have access to System.Net.Http.HttpClientFactory.
Am I meant to use HttpClientFactory.Create() to create HttpClients?
Or do I have to use DI and IHttpClientFactory?
What is HttpClientFactory used for and why doesn't it implement IHttpClientFactory?
I've checked the source code of the
DefaultHttpClientFactoryand even though it is part of theMicrosoft.Extensions.Httpnamespace it is marked asinternal.Gladly the
AddHttpClientextension method can do the DI registration of the above class on our behalf.So, all you need to do is:
ServiceCollectionAddHttpClientServiceProviderGetRequiredServiceIn order to do that in .NET Framework 4.7.2 you need to use the
Microsoft.Extensions.DependencyInjectionpackage.Here I have detailed how to do the same with
SimpleInjector