Tips and Tricks for HTTP Client in .Net core

Introduction

We use HTTPClient in .net to make http requests to another API to get data. In this article we will about HTTP client best usage.

There will be performance issue if we use httpclient in bad way. Consider a code below where it is required to create httpclient multiple times when we need data from another api.

We can use using statements for classes those implements IDisposable.

Consider below code


csharp
for(int i = 0; i<1000; i++) { using(var client = new HttpClient()) { var result = await client.GetAsync(“http://google.com"); Console.WriteLine(result.StatusCode); } }



In this case HttpClient, goes out of scope and is disposed. The dispose 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 stream writers.

All unmanaged code will be cleaned once using block is out of scope.

But with HttpClient case is different and have some problems here.  

Problems

Here we are creating httpclient object multiple time and which cause a problem called.

Socket Exhaustion Problems

This means when httpclient make connection to url mentioned, socket remain opens even when http connection get close. This problem not always happen but may happen when we create httpclient object frequently in applications.

Ideal should to use singleton object of httpclient and reuse it.

Therefore, HttpClient is intended to be instantiated once and reused throughout the life of an application

In screen shot 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 error like:

System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

TCP states diagram.

Solution:

a. Make httpclient singleton so that this will be reusable each and every time

b. We can make httpclient as static

c. We can use IHttpClientFactory

I will create another article on IHttpClientFactory

Fix Code below is solution

Console.WriteLine(“Starting connections”);

csharp
private static HttpClient Client = new HttpClient(); for(int i = 0; i<1000; i++) { var result = await Client.GetAsync(“http://google.com"); Console.WriteLine(result.StatusCode); } Console.WriteLine(“Connections done”); Console.ReadLine();

Understanding above code

a. We have created global object for httpclient, we should reuse it

b. We have not used using block for httpclient , please try to avoid it.

Conclusion

Using block for httpclient may create huge (even it implemented IDisposable Interface ) problems in production and can degrade server performance and results in slow server response.

line

Copyrights © 2024 letsupdateskills All rights reserved