The program makes extensive use of httpclient to establish connections with various external APIs in order to retrieve data from C# code.
However, it is necessary to ascertain the consequences of misusing this HTTP client. If we apply the method below, the program will not operate as intended.
Examine the C# code below.
Console.WriteLine("Starting connections");
for (int i = 0; i < 100; i++)
{
using (var client = new HttpClient())
{
var result = await client.GetAsync("http://google.com");
Console.WriteLine(result.StatusCode);
}
}
Output like below.
We can use statements for classes that implement IDisposable.
In this case, HttpClient goes out of scope and is disposed of. The disposal method is called, and whatever resources are in use are cleaned up. This is a very typical pattern in .NET, and we use it for everything from database connections to streamwriters.
All unmanaged code will be cleaned once the block is out of scope.
But with HttpClient, the case is different and has some problems here.
Problems
Here, we are creating httpclient object multiple times, which causes a problem called.
Socket exhaustion problems
This means when the HTTP client makes a connection to the url mentioned, the socket remains open even when the HTTP connection gets closed. This problem does not always happen, but it may happen when we create httpclient objects frequently in applications.
It is ideal to reuse a singleton object of an HTTP client and reuse it.
Therefore, HttpClient is intended to be instantiated once and reused throughout the life of an application.
In the screenshot below, I have used netstat.exe to investigate.
All TIME_WAIT state, which means that the connection has been closed on one side (ours i.e. .NET code side), but we’re still waiting to see if any additional packets come in on it because they might have been delayed on the network somewhere.
There is a limit to how quickly Windows can open new sockets, so if you exhaust the connection pool, then you’re likely to see errors like this.
System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Solution
- Make httpclient singleton so that this will be reusable each and every time.
- We can make httpclient static.
- We can use IHttpClientFactory.
I will create another article on IHttpClientFactory.
The fix Code below is the solution.
Understanding the above code.
- We have created a global object for httpclient
- We have not used using block for HTTP clients, please try to avoid it.
Conclusion
Using block for HTTP clients may create huge (even if it implemented IDisposable Interface ) problems in production and can degrade server performance and result in slow server responses.
Best ASP.NET 8.0.7 Core Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFEASP.NET, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.