I've got a .NET 4.8 windows forms app which uses Autofac for DI. Currently, we're using a single HttpClient
instance which is registered as a singleton:
builder.Register(c => {
var handler = c.Resolve<HttpClientHandler>();
var bearerHandler = new BearerAuthenticationDelegatingHandler( c.Resolve<AuthenticationManager>(), handler);
var client = new HttpClient(bearerHandler) {
BaseAddress = new Uri(baseUrl)
};
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
})
.AsSelf()
.SingleInstance();
AuthenticationManager
is responsible for managing the access tokens which are injected into the web requests (BearerAuthenticationDelegatingHandler
).
Even though I'm not being affected by DNS issues, I'd like to follow MS HttpClient
recommendations and I'd like to add IHttpClientFactory
to my app. However, it seems like there's simply no way to do that without adding a dependency to the dependency injection library which is typically used in .NET 7 projects (for instance, here's an example found of this approach).
So, is there any way to use IHttpClientFactory
in .NET 4.8 projects without having to depend on the DI extension library?
Thanks.