Friday, March 23, 2018

Overhead of Creating a new HttpClient per call/request - Exception : Unable to connect to the remote server System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Exception : Unable to connect to the remote server System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

“As Per Microsoft documentation, HTTP Client is intended to be instantiated once and it should be reused thought the life of application.”

Let evaluate the above Microsoft document statement about HttpClient, if you are creating a new HttpClient instance it means one TCP/IP connection is established with Server


using System.Net.Http;
HttpClient client = new HttpClient();

Creating a new HTTPClient instance per request, it can exhaust the available TCP socket and server comes under heavy load.

There is limited number for connection pool on Windows Server, if you have already used available connection or exhaust the connection pool then you encountered error:

Unable to connect to the remote server
System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted.

Disposing HttpClient instance by Using Statement:
Using Statement in C# is used for Disposable object and, if object goes out of using scope, it is disposed.

HttpClient is also disposable object and if we use using statement to instantiate HttpClient and when it goes out of scope, it is being disposed.

using (HttpClient client = new HttpClient())
           {

           }

In Disposing of HttpClient it close the active TCP/IP connection with Server but due to delay on the network there is always waiting time from other side and If you create more HttpClient instances it means you opened more TCP/IP connections, it could be very costly and more waiting time process to dispose HttpClient instance.


Conclusion is that for creating HttpClient instance per request by using “using” statement it could be more problematic.

It is advisable to use the single instance of HttpClient per Web Service throughout application. You can manage the single instance of HTTPClient by using the singleton Patterns or static class.

public sealed class CommonHttpClient
    {
        private static HttpClient instance;

        private static object syncRoot = new object();

        public static HttpClient Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            var handler = new HttpClientHandler { UseDefaultCredentials                                        = true, Credentials = CredentialCache.DefaultCredentials };
                            var client = new HttpClient(handler, false)
                                         {
                                             BaseAddress = newUri(“http://localhost”)
                                         };
                            instance = client;
                        }
                    }
                }

                return instance;
            }
        }

    }


Thanks for visiting!!


No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...