Is singleton a good way to instantiate an HttpClient

I have a C # library that makes calls for a relaxed web service using HttpClient.

The class library is used inside the MVC application, which can run for several weeks without restarting.

Is singleton a good way to create an HttpClient, so I only have one HttpClient for a very long lifetime MVC application?

0
source share
2 answers

I singleton will save the HttpClientapplication for life, but I would ask why you want to do this. This is not a good practice.

HttpClient IDisposable, , . using :

using (var client = new HttpClient()) 
{
  // Do something
}

, , , . HttpClient , .

0

HttpClient?

. HttpClient, :

"HttpClient - . HttpClient HTTP-, HttpClient, ."

, singleton HttpClient, , :

CancelPendingRequests
DeleteAsync
GetAsync
GetByteArrayAsync
GetStreamAsync
GetStringAsync
PostAsync
PutAsync
SendAsync

:

  • , ( eachother), HttpClient .
  • genral HttpClient .
0

All Articles