0

I have a Windows Service that sends json data to a MVC5 WebAPI using the WebClient. Both the Windows Service and the WebClient are currently on the same machine.

After it has run for about 15 minutes with about 10 requests per second each post takes unreasonably long to complete. It can start out at about 3 ms to complete a request and build up to take about 5 seconds, which is way too much for my application.

This is the code I'm using:

private WebClient GetClient()
{
var webClient = new WebClient();

webClient.Headers.Add("Content-Type", "application/json");
    return webClient;
}

public string Post<T>(string url, T data)
{
    var sw = new Stopwatch();
    try
    {
        var json = JsonConvert.SerializeObject(data);
        sw.Start();
        var result = GetClient().UploadString(GetAddress(url), json);
        sw.Stop();

        if (Log.IsVerboseEnabled())
            Log.Verbose(String.Format("json: {0}, time(ms): {1}", json, sw.ElapsedMilliseconds));

        return result;
    }
    catch (Exception)
    {
        sw.Stop();
        Log.Debug(String.Format("Failed to send to webapi, time(ms): {0}", sw.ElapsedMilliseconds));

        return "Failed to send to webapi";
    }
}

The result of the request isn't really of importance to me.

The serialized data size varies from just a few bytes to about 1 kB but that does not seem to affect the time it takes to complete the request.

The api controllers that receive the request completes their execution almost instantly (0-1 ms).

From various questions here on SO and some blog posts I've seen people suggesting the use of HttpWebRequest instead to be able to control options of the request.

Using HttpWebRequest I've tried these things that did not work:

Why are the requests taking so long? Any help is greatly appreciated.

Community
  • 1
  • 1
Johan Gov
  • 1,262
  • 1
  • 13
  • 26
  • Are you sure it's .NET that's doing this? Can you simulate and reproduce the behavior through another tool that does the same? Do you mean client and webservice are on the same machine? Can it be that you're out of sockets, and the code has to wait for a free socket? – CodeCaster Sep 23 '15 at 12:44
  • Please check if this isn't related to your problem: https://support.microsoft.com/en-us/kb/324270 The other day I had my Windows 2003 Server dropping localhost connections between two processes because of this security feature. – Kuba Wyrostek Sep 23 '15 at 12:45
  • @CodeCaster yes the client and the webservice is on the same machine. How do I check if I'm out of sockets? – Johan Gov Sep 24 '15 at 14:48

1 Answers1

0

It turned out to be another part of the program that took all available connections. I.e. I was out of sockets and had to wait for a free one.

I found out by monitoring ASP.NET Applications\Requests/Sec in the Performance Monitor.

Johan Gov
  • 1,262
  • 1
  • 13
  • 26