Using HttpClientFactory without dependency injection
See original GitHub issueIs 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:
- Created 4 years ago
- Reactions:70
- Comments:109 (35 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
Startupclass 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
ILoggerwithout 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.The default behavior of the
HttpClientshouldn’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 “usingHttpClientFactoryis 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 theHttpClientdocumentation. 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
HttpClientthat 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 configureHttpClient?No other language or runtime I use makes it this hard to write an application that needs to make HTTP requests.