0

I have read many documents and i saw many examples. But i didn't manage to understand exactly which is the proper way to use httpclient. My aplication uses httpclient multiple times in different threads and in different urls, and in most cases at the same time. Till now i am using httpclient as example below for each of my request.

using(var client = new HttpClient())
{
    client.TimeOut = TimeSpan.FromSeconds(20);
    var result = await client.GetAsync(www.mysite.com);
}

As Microsoft's documentation says "HttpClient is intended to be instantiated once per application, rather than per-use" Can i use a static instance of httpclient? Then to make a request without to affect an already active 'GET' request? Is it better by this way? Could please someone clarify me this?

I am using a winform application and a xamarin application.

Fildor
  • 14,510
  • 4
  • 35
  • 67
  • Disposing HttpClient forces all tcp connections to close. Keeping it alive will perform better for multiple requests to the same server. – Jeremy Lakeman Jun 15 '21 at 06:46
  • The recommendation is to in fact one single instance. Either through explicitly constructing one or (which I'd prefer) through [IHttpClientFactory](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests). But all of which also depends on what project type. Are you in a console app? – Fildor Jun 15 '21 at 06:47
  • Will i have any problem if i will make three requests in same time with different urls and timeouts? – Giorgos Papadopoulos Jun 15 '21 at 06:48
  • @Fildor no i am using a winform application and a xamarin application – Giorgos Papadopoulos Jun 15 '21 at 06:49
  • ^^ That would be good information to add to the _question_. (Edited for you :D) – Fildor Jun 15 '21 at 06:50
  • 1
    It's [explicitly documented](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0#remarks) that the various methods that send requests are thread safe. – Damien_The_Unbeliever Jun 15 '21 at 06:50
  • I am also using asp net framework. I don't know if it is matter... – Giorgos Papadopoulos Jun 15 '21 at 06:58
  • Could please someone write me an example of how i can use IHttpClientFactory with my exampel above? – Giorgos Papadopoulos Jun 15 '21 at 07:06
  • Does this answer your question? [Do HttpClient and HttpClientHandler have to be disposed between requests?](https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed-between-requests) – Dorin Baba Aug 27 '21 at 12:15

1 Answers1

1

You're correct you should make a single instance of the HttpClient for your application and use it in each of your threads. A popular way to do this is to use the Singleton pattern.

The case where you'd have multiple HttpClient instances in the same application would be if you want them to behave differently, for example you might want one that has a cookie store and one without.