简体   繁体   English

如何使用dotnet(nugget包)httpclient配置TCP连接数?

[英]How to configure number of TCP connections using dotnet (nugget package) httpclient?

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. 我曾经使用HttpWebRequest.ServicePoint.ConnectionLimit( http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit (v=vs.110) .aspx )来配置打开的TCP连接数由客户。

I am planning to use HttpCLient for my new Application. 我打算在我的新应用程序中使用HttpCLient。 However I couldn't find related information on how to configure number of TCP connections . 但是,我找不到有关如何配置TCP连接数的相关信息。 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. 我认为它将通过webrequesthandler中的一个属性公开( http://msdn.microsoft.com/en-us/library/system.net.http.webrequesthandler.aspx允许使用HttpClient的不受信任的SSL证书 )但是我不能找不到它。 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 ) 看起来它也无法通过自定义httpclient消息处理程序进行配置( http://www.asp.net/web-api/overview/advanced/httpclient-message-handlers

Question: 题:

How to configure number of TCP (persisted) connections? 如何配置 TCP(持久)连接数?

Update 1: 更新1:

Looks like it is not exposed through HttpClient (after analyzing the code through ILSpy). 看起来它没有通过HttpClient公开(在通过ILSpy分析代码之后)。 However, looks like i can use FindServicePoint method of ServicePointManager. 但是,看起来我可以使用ServicePointManager的FindServicePoint方法。 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? 任何人都可以确认这是否可行 - 通过查找我的uri的服务点并设置连接限制?

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

Update 2: 更新2:

In fact, looks like that is the way to go as HttpWebRequest.ServicePoint internally is invoking the same method. 实际上,HttpWebRequest.ServicePoint在内部调用相同的方法看起来就是这样。 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. 下面是来自ILSpy的相应代码片段 - 但是有相当多的servicepointmanager.findservicepoint(..)重载方法 - 选择正确的方法可能会很棘手。

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. 注意:我不想使用ServicePointManager。 DefaultConnectionLimit as its global . DefaultConnectionLimit作为其全局 I am looking for something related to httpwebrequest.servicepoint.connectionlimit which is local to the request and effects per service point (host). 我正在寻找与httpwebrequest.servicepoint.connectionlimit相关的东西, 它是请求和每个服务点(主机)的效果的本地

Regards. 问候。

You can use SemphoreSlim to control the entry points in your code that creates the request which in turn creates the TCP ports. 您可以使用SemphoreSlim来控制代码中创建请求的入口点,从而创建TCP端口。

How to limit the amount of concurrent async I/O operations? 如何限制并发异步I / O操作的数量?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM