3

I used to use HttpWebRequest.ServicePoint.ConnectionLimit (http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit(v=vs.110).aspx) to configure number of TCP connections opened by client.

I am planning to use HttpCLient for my new Application. However I couldn't find related information on how to configure number of TCP connections. I thought it would be exposed through one of the properties in webrequesthandler (http://msdn.microsoft.com/en-us/library/system.net.http.webrequesthandler.aspx, Allowing Untrusted SSL Certificates with HttpClient) however I couldn't find it. Looks like it can't be configured through a custom httpclient message handler as well (http://www.asp.net/web-api/overview/advanced/httpclient-message-handlers)

Question

How to configure number of TCP (persisted) connections?

Update 1

Looks like it is not exposed through HttpClient (after analyzing the code through ILSpy). However, looks like i can use FindServicePoint method of ServicePointManager. Can any one please confirm if this is the way to go - by finding the service point for my uri and setting the connection limit?

public static ServicePoint FindServicePoint(Uri address)
{
    return ServicePointManager.FindServicePoint(address, null);
}

Update 2

In fact, looks like that is the way to go as HttpWebRequest.ServicePoint internally is invoking the same method. Below is the corresponding code snippet from ILSpy - however there are quite a few servicepointmanager.findservicepoint(..) overload methods - picking the right one may be tricky.

Monitor.Enter(this, ref flag);
            if (this._ServicePoint == null || forceFind)
            {
                if (!this.ProxySet)
                {
                    this._Proxy = WebRequest.InternalDefaultWebProxy;
                }
                if (this._ProxyChain != null)
                {
                    this._ProxyChain.Dispose();
                }
                this._ServicePoint = ServicePointManager.FindServicePoint(this._Uri, this._Proxy, out this._ProxyChain, ref this._AbortDelegate, ref this.m_Aborted);
                if (Logging.On)
                {
                    Logging.Associate(Logging.Web, this, this._ServicePoint);
                }
            }

Note

I do not want to use ServicePointManager.DefaultConnectionLimit as it's global. I am looking for something related to httpwebrequest.servicepoint.connectionlimit which is local to the request and effects per service point (host).

halfer
  • 19,824
  • 17
  • 99
  • 186
Dreamer
  • 3,371
  • 2
  • 34
  • 50
  • Yes the global value affects HttpClient. Why don't you want to use ServicePointManager.DefaultConnectionLimit ? – Darrel Miller Sep 06 '14 at 01:20
  • @Darell, thanks for the confirmation (btw, i have looked at the code through ILSpy last week to confirm the same - removed that statement now from the qtn to avoid irrelevant info). I didn't want to use ServicePointMnager.Defaultconnectionlimit as it has global impact. My app talks with multiple services - so wanted to control service points locally for each of them (just like the way httpwebrequest.servicepoint). But, it is not a deal breaker - as i can use FindServicePoint(...) method. – Dreamer Sep 07 '14 at 03:12

1 Answers1

0

You can use SemphoreSlim to control the entry points in your code that creates the request which in turn creates the TCP ports.

How to limit the amount of concurrent async I/O operations?

Community
  • 1
  • 1
Kallol
  • 264
  • 1
  • 12