I have a WebAPI 2 app under .NET Fw 4.7 that needs to be converted from singleton HttpClient
implementation to HttpClientFactory
.
I'm trying to use the Microsoft.Extensions.Http library, but I am running into issues on how to actually use it. I followed a couple of examples, but most of them refer to .NET Core, which is not an option at this time.
The closest example I was able to find is Use HttpClientFactory from .NET 4.6.2, but that doesn't give a full example either.
I think a lot of issues I'm having are because of my relative ignorance of DI, and how to properly use it in WebAPI space.
Going with the code from the above link:
var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
var client = httpClientFactory.CreateClient();
How do I implement this so I can access CreateClient()
in a controller (or other classes) properly?