1

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?

Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
Not_Sure
  • 87
  • 1
  • 10
  • Can you share with us please one of your Controller's relevant part to see what does *ignorance of DI* mean? – Peter Csala Jul 29 '20 at 12:44
  • please try to use this repo as this can be used as same way https://github.com/uhaciogullari/HttpClientFactoryLite – Akhil Aug 13 '20 at 09:59

0 Answers0