0

I have a simple windows service (C#) that uses a timer to check an external API every 15 mins.

The service runs fine until the machine sleeps, then after a few more iterations (about 40 mins in) it loses internet and all Requests time-out. This behavior is reproducible across machines, accounts, etc. and doesn't recover until the service is restarted.

The odd thing, if we leave Fiddler running on the machine, this doesn't happen - the service will run for days with no issues.

We've tried running it under custom/admin accounts, NetworkService and LocalService accounts, allow the user to operate as OS, power-saving configs on the nic, trying multiple machines (7 and Server 08), port all code to a WinForms app - all with the same behavior.

I'm pulling hair out and hoping the "works with Fiddler" will raise eyebrows from someone in the know... My next test is to write a "controller service" that stops/restarts the first service but I hate that solution.

*updated, here's the code that makes API requests

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uriDomain + uriLogin);
    WebResponse resp = req.GetResponse();

and downloads available files

    using (WebClient client = new WebClient())
    {
        client.Headers["Cookie"] = persistCookie;
        client.DownloadFile(uriDomain + fileName, localStoragePath + fileName);
    }

The .net CLR should recycle connections - there's no explicit close, etc. for these objects. Further, I don't understand how running Fiddler causes everything to work fine.

thanks

  • Specifically *this* service? Do other things continue to work? Does your service hold a socket open, and that's failing, or does it fail to connect new sockets after a while? What error values/behaviour do you see? – Roger Lipscombe Oct 01 '17 at 18:12
  • The service continues to run and continues to attempt to connect to the external API but 40 mins after the machine sleeps, all attempts timeout. Other apps will run but even a batch script that pings an external domain will lose internet after those 40ish mins. – Matthew Barton Oct 01 '17 at 19:07
  • If the batch script (pinging an external domain) is running but your service isn't, does the problem still occur? – Harry Johnston Oct 02 '17 at 01:11

1 Answers1

0

Sounds like you're running out of connections.

A quick test would be to change the time from 15 minutes to 1 minute then repeat the test. If it fails faster, then there's a fair chance that the service isn't correctly closing connections. Eventually you'll hit the limit.

Xyrus
  • 34
  • 4
  • Thanks - I've tried 5, 10, 15 min intervals, it's always after appx 40 mins - independent of how many requests have been made. Each interval will make between 2 and upwards 30 requests (downloading files, etc.) All are fine but once that 40 mins has passed, all requests fail. – Matthew Barton Oct 01 '17 at 18:54