Hi,
I am using HttpClient in our ASP.NET core application but i found this article has the below note.
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Especially in server applications, creating a new HttpClient instance for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.
Then after more search i found another article .
According to the articles the solution is to use single instance of HttpClient throughout the life of an application, so I decided to use Singleton DI for HttpClient but again there is an issue with singleton regarding DNS change according to this issue .
So my question what will be the best way to use HttpClient in ASP.NET Core.
Thanks.
So my question what will be the best way to use HttpClient in ASP.NET Core.
The best way, in general, is to use a single instance.
I've never had any problems with DNS resolution and HttpClient, but that might not always be the case, as you can see in the issue you linked.
So the answer, as usual, is "it depends". If you're hitting servers that frequently change IP adresses, it might be a problem. In that case, it might be an idea to do some kind of HttpClient management, where you periodically swap and return a new instance.
@khellang Thanks a lot for your answer 馃憤
Most helpful comment
The best way, in general, is to use a single instance.
I've never had any problems with DNS resolution and
HttpClient, but that might not always be the case, as you can see in the issue you linked.So the answer, as usual, is "it depends". If you're hitting servers that frequently change IP adresses, it might be a problem. In that case, it might be an idea to do some kind of
HttpClientmanagement, where you periodically swap and return a new instance.