-1

The MS docs say "HttpClient is intended to be instantiated once and re-used throughout the life of an application" - this seems fair enough but not so simple when you are using HttpClient from within a web app. All the examples out there using HttpClient are done with console apps and infact i cant find any examples with a web app consuming an API using HttpClient. So my question is whether HttpClient is actually meant to be used from a web app or should i be using WebRequest or something else? Thanks to anyone who replies!

Addition 24hrs later....its a .net framework app. Sorry i only just catching up with the answers, i have never used this site before and assumed i would get an email when answers posted - sincere thanks for all your comments and looks like i have some reading to do

RichA
  • 3
  • 2
  • The example given for that very guideline in the [MS Doc](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1) is an example of web app code. That code stores the `HttpClient` in a static readonly field in an `ApiController`. – JLRishe Apr 26 '20 at 15:05
  • You can use it from web app, if you are working with an IOC library you can set this as a singleton and only 1 instnce will be created and injected when you need it. – Aviad Hasidof Apr 26 '20 at 15:05
  • .NET Framework or .NET Core? this links shows you how to use HttpClient in an ASP.NET Core application https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1 – Jesús López Apr 26 '20 at 15:20
  • fair point JLRiche but its just for one controller, so presumably each controller has its own static instance?? which, for me, still doesnt quite meet the MS line of "instantiated once and re-used throughout the life of an application" – RichA Apr 27 '20 at 10:22

3 Answers3

1

Thw preferred way is to use HttpClientFactory

You can then simply inject HttpClient in your Services and let the DI container worry about the lifetimes.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Console apps are often used for tutorial projects because they are easier to create and run without the added complexities involving service hosting. It is easier to use a console project to illustrate the functionalities of HttpClient than from inside a web application. Also, it is worth mentioning that all .NET Core apps are console applications.

In the production environment, web apps are hosted on web servers, containers, windows services or in server-less environment so managing the life cycle of the HttpClient is important and following Microsoft's recommendation makes sense here. You will most definitely be using an IoC container for managing the object graph in your applications. You can configure the IoC so the life cycle of the HttpClient is either a singleton or instantiated per HTTP request.

Stack Undefined
  • 1,050
  • 1
  • 14
  • 23
0

If you are using dotnet core then use HttpClientFactory if not use a private static field for your HttpClient. These are the recommended best practices. The second option still poses some issues but they are more edge cases and won't end up exhausting your sockets. This article explain s things very nicely https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/

Andrew
  • 5,215
  • 1
  • 23
  • 42
  • I would recommend using the build-in IoC container and injecting the HttpClient rather than creating a static field for unit testing and extensibility reasons. – Stack Undefined Apr 26 '20 at 17:53
  • As I said with dotnet core that would be the approach but the original question doesn't suggest that the user is using. Net framework or dotnet core. – Andrew Apr 26 '20 at 18:09
  • sorry Andrew for not clarifying i was using normal .net framework web app - that article was what i first stumbled across that got me wondering about all this, and it too uses a simple console app. Sounds like i can have a static class with a static instance of HttpClient which i can use from anywhere in my website - that just feels weird/unsafe to me as for each request i will be changing the bearer token - guess i just need to try it! – RichA Apr 27 '20 at 10:33
  • alaying my fears on previous comment..........https://stackoverflow.com/questions/37928543/httpclient-single-instance-with-different-authentication-headers – RichA Apr 27 '20 at 11:02