Using HttpClientFactory without dependency injection

See original GitHub issue

Is there any way to consume the HttpClientFactory without using dependency injection, or more precisely without using M.E.DI?

I am creating a client library for which I want to make use of HttpClientFactory but I am going to use this library also in a legacy project that does not not have a M.E.DI-compatible DI.

Ideally, I would just new up a DefaultHttpClientFactory and consume that directly but since the factory itself is internal, this is not possible.

Why is the HttpClientFactory internal and is there any way to consume it explicitly without using DI?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:70
  • Comments:109 (35 by maintainers)

github_iconTop GitHub Comments

52reactions
alexeyzimarevcommented, Jul 15, 2019

A. I want an opinionated builder abstraction for configuring HttpClientFactory. B. I don’t need that. I just want an HttpClient with good network behavior by default.

I’d like to stress out that using the DI container for absolutely everything is not the best example of design. Insisting that all developers need to use the container for things like HTTP client factory, in-memory cache, distributed cache, logging and so on, creates impediments for developers that would otherwise avoid using DI containers altogether. I personally prefer to construct my dependency tree in the Startup class explicitly and don’t rely on something else that I don’t control to resolve dependencies for me.

There’s was an issue https://github.com/aspnet/Extensions/issues/615 for using ILogger without the container and it is fixed for .NET Core 3. I would expect that all other factories would follow the same approach and expose a way to instantiate factories without using the container, at least for the reason of consistency.

51reactions
SittenSpynnecommented, Jul 22, 2020

I hear you but those Dns problems are solved by tweaking either service point manager (on .NET Framework) or by tweaking the setting non SocketsHttpHandler on .NET Core. If that doesn’t work then we’d need to do fix those issues in the platform (at least on .NET Core). If you have evidence that this doesn’t work, then we’d love to hear it. The HttpClientFactory is meant to be a configuration API, not a lifetime management API. In fact, we’re likely going to remove the handler pooling and instead set the appropriate settings based on the platform to avoid this confusion.

The default behavior of the HttpClient shouldn’t require navigating gotchas that I have to spend all day reading blog articles to solve. Once I’ve read 3 blog articles I have 5 different solutions, 2 of them tell me “using HttpClientFactory is the easiest way to get it right”. The other 3 warn me if I’m not an experienced networking engineer, I might do more harm than good. None of these issues are in the HttpClient documentation. It’s the Tomb of Horrors of pits of failure.

I’ve been using .NET since 2003 and I still worry every time I use HttpClient that I’m missing some ancestral knowledge because I didn’t follow the right blog post. It’d be nice if MS retooled it to be right by default and let me tweak it if I have special needs and understand what those needs are. It’s possible it works better in .NET Core? I have to configure it differently there. What’s that mean for Xamarin? That’s sort of .NET Core but actually Mono, right? What’s that going to mean for MAUI in .NET 5? Will I have to learn four ways to configure HttpClient?

No other language or runtime I use makes it this hard to write an application that needs to make HTTP requests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using HttpClientFactory without dependency injection
Is there any way to consume the HttpClientFactory without using dependency injection, or more precisely without using M.E.DI?
Read more >
Making HTTP Call without Dependency Injection in .NET ...
1 Answer 1 ... You don't need to inject a typed HttpClient , or use IHttpClientFactory . It is recommended to solve certain...
Read more >
Tag: httpclientfactory without dependency injection
Configure HttpClientfactory using Autofac DI Today in this article, we will see how to use HttpClientfactory by configuring it using Autofac DI Container...
Read more >
Use IHttpClientFactory to implement resilient HTTP requests
Injected HttpClient instances are Transient from a DI perspective, while HttpMessageHandler instances can be regarded as Scoped.
Read more >
HttpClient in .NET– is there a best way - eidias
The only requirement is to use dependency injection. First create new console app project and import two nuget packages Microsoft.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found