1

I have windows service application which should be communicating with other server. Problem is that it works for a while after I restart the windows service but after some time it stops working (response.StatusCode = 0).

var client = new RestClient("https://..../auth/token")
{
    Proxy = new WebProxy(host, port)
};

var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");

var content = new
{
    sp = sp,
    officeId = officeId
};

request.AddJsonBody(_javaScriptSerializer.Serialize(content));

var response = client.Execute(request);

Windows service is implemented in .NET Framework 4.5.2. Security protocol for TLS 1.2 is enabled:

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

I created dummy windows service which contains only this 1 call to the other server and I can see that this dummy windows service is working fine without any issues.

Do you have please any idea on what should I focus or what could be the root cause of it?

Regi
  • 11
  • 1
  • `RestClient` is based on `System.Net.Http.HttpClient`. Please [take a look at the docs](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0#remarks), and read the 'Remarks' section carefully. Best practice would be to have a single instance and reuse it. My guess is, that you run out of connection pools after some time, when you create a `RestClient` instance every time you make a request. – nilsK May 27 '21 at 11:21
  • `RestClient` does not use connection pool as `HttpClient` and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of `RestClient` per request. copied from: https://stackoverflow.com/questions/49588205/should-restclient-be-singleton-or-new-for-every-request – Regi May 31 '21 at 10:34

0 Answers0