How can I configure the number of TCP connections using dotnet (nugget packet) httpclient?

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

I am planning to use HttpCLient for my new application. However, I could not find any relevant information on setting the number of TCP connections . I thought this would be shown via one of the properties in the webrequesthandler ( http://msdn.microsoft.com/en-us/library/system.net.http.webrequesthandler.aspx , Allow untrusted SSL certificates with HttpClient ), however I couldn't find it. It looks like it cannot be configured with a custom httpclient message handler ( http://www.asp.net/web-api/overview/advanced/httpclient-message-handlers )

Question:

How do I configure the number of TCP (persistent) connections?

Update 1:

It looks like it doesn't show up via HttpClient (after parsing the code via ILSpy). However, it looks like I can use the FindServicePoint method on the ServicePointManager. Can anyone confirm if this is the way to go - find a service point for my uri and set a connection limit?

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

      

Update 2:

It actually looks like this: HttpWebRequest.ServicePoint internally calls the same method. Below is the relevant code snippet from ILSpy - however there are many servicepointmanager.findservicepoint (..) overloading methods - choosing the right one can 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 don't want to use the ServicePointManager. DefaultConnectionLimit as global . I'm looking for something related to httpwebrequest.servicepoint.connectionlimit that is local to request and effects for each service point (host).

Sincerely.

+3


source to share


1 answer


You can use SemphoreSlim to manipulate entry points in your code, which creates a request, which in turn creates TCP ports.



How do I limit the number of concurrent asynchronous I / O operations?

0


source







All Articles