none
ServicePointManager.DefaultConnectionLimit hangs the thread and timesout RRS feed

  • Question

  • Team,

    We have 5 prod servers (.net 4.5) and receiving 2 Million request per day, we are seeing lot of timeouts to the API calls.

    We have DefaultConnectionLimit = 100, is this number sufficient or we need to increase this further.

    Is this the capacity issue...appreciate your response



    Software Engineer

    Wednesday, July 11, 2018 9:08 AM

All replies

  • Hi Maria Sagayaraj,

    Thank you for posting here.

    Based on my search, here are two ways for your reference.

    1. The code you use is okay. You could set a larger number.

    ServicePointManager.DefaultConnectionLimit = 1000;

    Please remember, this line must be executed before the first time a request to target server is made. Otherwise, it will be set to default value 2.

    2. Add a few lines in App.config file.

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
      <system.net> 
        <connectionManagement> 
          <add address="*" maxconnection="1000" /> 
        </connectionManagement> 
      </system.net> 
    </configuration> 

    For more details, please refer to the MSDN similar thread.

    https://social.msdn.microsoft.com/Forums/en-US/1f863f20-09f9-49a5-8eee-17a89b591007/asynchronous-httpwebrequest-maximum-connections-best-approach-threads-or-delegates?forum=netfxnetcom

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 13, 2018 7:25 AM
    Moderator
  • Thanks for your reply, i set the DefaultConnectionLimit = 4000, but still seeing the error from Dump as

    HttpRequest URI : http://proxyurl

    ServicePoint - ConnectionLimit : 4000 CurrentConnections:4973

    Even after setting the option1, should we still set the maxconnection (option 2) ?

    Friday, July 13, 2018 5:17 PM

  • I have 2 options now.

    1. When the IPAddress is wrong, it takes 75 secs to timeout and that holds the thread which causes the thread limitations, if i can reduce this time it will have more threads to serve

    2. If i have more threads available then that should also solve, but i have increased to 4000, still i get the error as stated before.

    This is my code

        {
            HttpWebRequest ObjHttpWebRequest = null;
            HttpWebResponse objHttpWebResponse = null;
            Stopwatch watch = new Stopwatch();
            string strReturn = string.Empty;
            string validUrl = "false";
            try
            {
                watch.Start();
                string DATA = "postdata";
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(MyCertValidationCb);
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                ObjHttpWebRequest = (HttpWebRequest)WebRequest.Create("https://196.234.502.247:4510/path");
                ObjHttpWebRequest.Timeout = 15000;
                ObjHttpWebRequest.Method = "POST";
                ObjHttpWebRequest.ContentType = "application/json";
                ObjHttpWebRequest.ContentLength = DATA.Length;
                {
                    WebProxy proxy = new WebProxy();
                    proxy.Address = new Uri("http://proxy.com");
                    proxy.UseDefaultCredentials = true;
                    ObjHttpWebRequest.Proxy = proxy;
                }
                X509Certificate2 X509cert2_client = new X509Certificate2(HttpContext.Current.Request.PhysicalApplicationPath + @"\certs\test.pfx", "test");
                ObjHttpWebRequest.ClientCertificates.Add(X509cert2_client);

                byte[] byteArray = Encoding.UTF8.GetBytes(DATA);
                ObjHttpWebRequest.ContentLength = byteArray.Length;
                Stream objRequestStream = ObjHttpWebRequest.GetRequestStream();
                objRequestStream.Write(byteArray, 0, byteArray.Length);
                objRequestStream.Close();

                using (objHttpWebResponse = ObjHttpWebRequest.GetResponse() as HttpWebResponse)
                {
                    using (var objResponseStream = objHttpWebResponse.GetResponseStream())
                    {
                        using (var objResponseStreamReader = new StreamReader(objResponseStream, Encoding.UTF8))
                        {
                            strReturn = objResponseStreamReader.ReadToEnd();
                            objResponseStreamReader.Close();
                        }
                        objResponseStream.Close();
                    }
                    objHttpWebResponse.Close();
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
            finally
            {
                watch.Stop();
                if (ObjHttpWebRequest != null) ObjHttpWebRequest.Abort();
                if (objHttpWebResponse != null) objHttpWebResponse.Close();
                HttpContext.Current.Response.Write(strReturn+ "--"+ watch.Elapsed.TotalSeconds.ToString()+"__"+ validUrl);
            }
        }



    Software Engineer

    Sunday, July 15, 2018 2:09 PM
  • We moved to different server list and the issue got resolved.

    And as well i was connected via proxy and the timout set in the proxy is not controlled by us, so any wrong IP was taking close to 75 Secs timeout and that hanged the threads.


    Thursday, July 26, 2018 11:55 AM
  • If this is a web API or web site then please post in the ASP.NET forums.  IIS isn't going to be impacted by ServicePointManager. Only clients would be.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, July 26, 2018 2:15 PM
    Moderator
  • This is a WCF rest service.

    After 10  days the issue started again in the new datacenter servers, i am clueless whats happening.

    Is this because proxy server has limitations is serving the request or an outbound request from my code to the wrong URI has limitations, can someone confirm please.


    Saturday, August 4, 2018 7:13 PM
  • If the timeouts are on the client side then the connection timeout stuff has to be over there. It has no impact on a WCF service hosted in IIS. For IIS related issues please post in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, August 4, 2018 10:15 PM
    Moderator