Posts

Showing posts from August, 2024

ASP.NET Core: Using HTTPCLIENT with a Correct Way

Image
If you are building .NET Applications, then certainly your application needs to perform external API calls using HTTP Protocol.  Perhaps, if I say that the HTTP calls is a data fetching backbone of your applications then I won't be wrong. In such scenarios to perform HTTP calls we have the HttpClient class in .NET. The HttpClient class offers a great abstraction over managing HTTP Communications especially when the data communication is in the form of the JSON. The Figure 1 explains the scenario of the HTTP API calls. Figure 1: The HttpClient to Access HTTP APIs   But here are some possibilities where the HttpClient may be misused: If the new HttpClient instance is created every time, then that may result into port exhaustion.  Since the HttpClient implements IDisposable interface, we can use the HttpClient in the using statement for disposing its instance, but the problem is even after the HttpClient instance is disposed the underlying socker is not immediately release and this si