4

Using ASP.NET MVC with NET 4.5. There is a singleton HttpClient instance which is used to communicate with another ASP.NET WebAPI application.

I have noticed a large number of connections towards the port used by the WebAPI app. It was weird, considering I use a single HTTP client instance.

To investigate the issue, I set the connection limit prior to creating the HTTP client like this:

// baseAddress is "http://localhost:1900/api/"
var sp = ServicePointManager.FindServicePoint(new Uri(baseAddress));

// maxConnections = 20
sp.ConnectionLimit = maxConnections;

I created another method which returns the ServicePoint instance for the given endpoint. And after some hundred requests, I invoked this method to see what's going on. Here is the result:

{
    BindIPEndPointDelegate: null,
    ConnectionLeaseTimeout: 60000,
    Address: "http://localhost:1900/api/",
    MaxIdleTime: 100000,
    UseNagleAlgorithm: true,
    ReceiveBufferSize: -1,
    Expect100Continue: true,
    IdleSince: "/Date(1518431339114)/",
    ProtocolVersion: {
        Major: 1,
        Minor: 1,
        Build: -1,
        Revision: -1,
        MajorRevision: -1,
        MinorRevision: -1
    },
    ConnectionName: "http",
    ConnectionLimit: 20,
    CurrentConnections: 560,
    Certificate: null,
    ClientCertificate: null,
    SupportsPipelining: true
}

Note how CurrentConnections is significantly larger than ConnectionLimit. What gives, any clue?

Dejan Janjušević
  • 3,181
  • 4
  • 41
  • 67
  • Haven't tried to recreate this but two quick shots: 1) aren't you setting the `DefaultConnectionLimit` somewhere else prior to setting `ConnectionLimit` also 2) aren't you setting the higher value prior to setting lower value (I remember specific issues with **lowering** the limit, can't remember details at the moment, though)? – Wiktor Zychla Feb 12 '18 at 18:59
  • @WiktorZychla I have tried setting both `DefaultConnectionLimit` and `ConnectionLimit` but to no avail. Besides, seemed logical, setting the `ConnectionLimit` would override the default value. On the other hand, nice hint! Since I inherited the maintenance of this application, what you wrote made me realize the previous maintainers probably tried to meddle with this setting, so guess what, there was `ServicePointManager.DefaultConnectionLimit = int.MaxValue;` in `Application_Start`. Gotta be related to that lowering issue you mentioned. Posting back tomorrow when I get back to the office! – Dejan Janjušević Feb 12 '18 at 20:09
  • @WiktorZychla after removing this `int.MaxValue` thing it still behaves the same. Moreover, if I set a breakpoint prior to setting the desired `ConnectionLimit`, i can see it's already set to max value, even though I am sure it's not deliberately set in my code. I found this though: https://stackoverflow.com/a/36835795/828023 - it appears that the framework sets it to max value (!) – Dejan Janjušević Feb 13 '18 at 08:27
  • That sounds unfortunate, the issues we had and I mentioned were in a system service (which of course doesn't use the ASP.NET stack and therefore doesn't call this method that would set the limit). I recommend trying the same in a console app and if it works, you possibly would be forced to rethink the approach as you can't do anything about the limit. – Wiktor Zychla Feb 13 '18 at 09:51

0 Answers0