0

I'm trying to send multiple API requests from C# webservice on every 5 minutes using HttpClient. After some time all sockets are exhausted because they are not closing after request is sent. How I can close all w3wp.exe sockets from web service method?

private static HttpClient client = new HttpClient();
public async Task sendReq()
{
    var url = "http://url.com";
    var sp = ServicePointManager.FindServicePoint(new Uri(url));
    sp.ConnectionLeaseTimeout = 20 * 1000;

    client.DefaultRequestHeaders.ConnectionClose = true;
    client.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
    client.DefaultRequestHeaders.Add("Keep-Alive", "300");
    client.DefaultRequestHeaders.ConnectionClose = true;
    var res = await client.GetStringAsync(url);
}
  • 2
    By not disposing the HttpClient. – CodeCaster Jan 26 '21 at 11:18
  • @CodeCaster I don't dispose it. It is defined only once – Vasil Gorgiev Jan 26 '21 at 11:21
  • 2
    Please provide all relevant code and details. Socket exhaustion is caused by HttpClient not reusing connections. If you send tens of thousands of requests at the same time, then there's no way around waiting a bit until all lingering sockets are closed. – CodeCaster Jan 26 '21 at 11:23
  • @CodeCaster I updated the question. I'm using it in Web Service and on every web service request it creates new HttpClient instance. So, how to reuse existing connection in Web Service? – Vasil Gorgiev Jan 26 '21 at 11:27
  • What version of .Net is this? There are big differences in .Net framework and .Net Core around this area – Liam Jan 26 '21 at 11:27
  • Does this answer your question? [HttpClient and the socket Exhaustion - clarification?](https://stackoverflow.com/questions/58427764/httpclient-and-the-socket-exhaustion-clarification) – Liam Jan 26 '21 at 11:29
  • @Liam it is in .NET Framework 4.5.2 – Vasil Gorgiev Jan 26 '21 at 11:30
  • 1
    A static HttpClient should live as long as your application pool does. Are you issuing thousands of requests to different hosts? – CodeCaster Jan 26 '21 at 11:36
  • @CodeCaster thousands requests to same host. Only change in URL is the value of query string parameters. I think on every request to Web Service method it creates new HttpClient – Vasil Gorgiev Jan 26 '21 at 11:41
  • 1
    Requests to the same host should reuse the existing connection unless you do something else. Again, provide all code and relevant details in your question by [edit]ing it. – CodeCaster Jan 26 '21 at 11:54
  • @CodeCaster thank you for your time. I added more code related to this. Again, it is in Web Service method, not i.e. standard desktop app. How to reuse HttpClient in Web Service? – Vasil Gorgiev Jan 26 '21 at 11:58
  • Why do you set the connection: close header _and_ the keep-alive header? Pick either. In other words, remove both `client.DefaultRequestHeaders.ConnectionClose = true`. – CodeCaster Jan 26 '21 at 12:07
  • @CodeCaster Already tried, but same. Sockets aren't closed after request – Vasil Gorgiev Jan 26 '21 at 12:15

0 Answers0