.net 如何以编程方式删除 WebClient 中的 2 个连接限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/866350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:42:25  来源:igfitidea点击:

How can I programmatically remove the 2 connection limit in WebClient

.nethttpconnectionwebclientlimit

提问by Christian

Those "fine" RFCs mandate from every RFC-client that they beware of not using more than 2 connections per host...

那些“良好”的 RFC 要求每个 RFC 客户端注意不要在每个主机上使用超过 2 个连接......

Microsoft implemented this in WebClient. I know that it can be turned off with

微软在 WebClient 中实现了这一点。我知道它可以关闭

App.config:

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
 <system.net> 
  <connectionManagement> 
   <add address="*" maxconnection="100" /> 
  </connectionManagement> 
 </system.net> 
</configuration> 

(found on http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007)

(在http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/1f863f20-09f9-49a5-8eee-17a89b591007 上找到)

But how can I do it programmatically?

但是我怎样才能以编程方式做到这一点?

Accordin to http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

根据 http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx

"Changing the DefaultConnectionLimit property has no effect on existing ServicePoint objects; it affects only ServicePoint objects that are initialized after the change. If the value of this property has not been set either directly or through configuration, the value defaults to the constant DefaultPersistentConnectionLimit."

“更改 DefaultConnectionLimit 属性对现有 ServicePoint 对象没有影响;它仅影响更改后初始化的 ServicePoint 对象。如果未直接或通过配置设置此属性的值,则该值默认为常量 DefaultPersistentConnectionLimit。”

I'd like best to configure the limit when I instanciate the WebClient, but just removing this sad limitation programmatically at the start of my programm would be fine, too.

我希望在实例化 WebClient 时最好配置限制,但是在我的程序开始时以编程方式删除这个可悲的限制也可以。

The server I access is not a regular webserver in the internet, but under my control and in the local lan. I want to do API-calls, but I don't use webservices or remoting

我访问的服务器不是 Internet 中的常规 Web 服务器,而是在我的控制下和本地局域网中。我想做 API 调用,但我不使用网络服务或远程处理

采纳答案by Shizam

With some tips from here and elsewhere I managed to fix this in my application by overriding the WebClient class I was using:

通过这里和其他地方的一些提示,我设法通过覆盖我正在使用的 WebClient 类在我的应用程序中解决了这个问题:

class AwesomeWebClient : WebClient {
    protected override WebRequest GetWebRequest(Uri address) {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);
        req.ServicePoint.ConnectionLimit = 10;
        return (WebRequest)req;
    }
}

回答by lilmoe

for those interested:

对于那些有兴趣的人:

System.Net.ServicePointManager.DefaultConnectionLimit = x(where x is your desired number of connections)

System.Net.ServicePointManager.DefaultConnectionLimit = x(其中 x 是您想要的连接数)

no need for extra references

不需要额外的参考

just make sure this is called BEFORE the service point is created as mentioned above in the post.

只需确保在创建服务点之前调用它,如上文所述。

回答by George Tsiokos

This solution allows you to change the connection limit at any time:

该解决方案可让您在更改连接限制的任何时间

private static void ConfigureServicePoint(Uri uri)
{
    var servicePoint = ServicePointManager.FindServicePoint(uri);

    // Increase the number of TCP connections from the default (2)
    servicePoint.ConnectionLimit = 40;
}

The 1st time anyone calls this FindServicePoint, a ServicePointinstance is created and a WeakReferenceis created to hold on to it inside the ServicePointManager. Subsequent requests to the manager for the same Uri return the same instance. If the connection isn't used after, the GC cleans it up.

第一次调用此FindServicePoint 时,会创建一个ServicePoint实例并创建一个WeakReference以在ServicePointManager 中保留它。对相同 Uri 的后续请求返回相同的实例。如果此后未使用该连接,GC 将对其进行清理。

回答by Katelyn Gadd

If you find the ServicePoint object being used by your WebClient, you can change its connection limit. HttpWebRequest objects have an accessor to retrieve the one they were constructed to use, so you could do it that way. If you're lucky, all your requests might end up sharing the same ServicePoint so you'd only have to do it once.

如果发现 WebClient 正在使用 ServicePoint 对象,则可以更改其连接限制。HttpWebRequest 对象有一个访问器来检索它们被构造使用的那个,所以你可以这样做。如果幸运的话,您的所有请求最终可能会共享同一个 ServicePoint,因此您只需执行一次。

I don't know of any global way to change the limit. If you altered the DefaultConnectionLimit early enough in execution, you'd probably be fine.

我不知道任何改变限制的全局方式。如果您在执行时足够早地更改 DefaultConnectionLimit,您可能会没事。

Alternately, you could just live with the connection limit, since most server software is going to throttle you anyway. :)

或者,您可以忍受连接限制,因为无论如何大多数服务器软件都会限制您。:)

回答by Teo-Kostas

We have a situation regarding the above piece of configuration in App.Config

我们在 App.Config 中有一个关于上述配置的情况

In order for this to be valid in a CONSOLE Application, we added the System.Configuration reference dll. Without the reference, the above was useless.

为了使其在 CONSOLE 应用程序中有效,我们添加了 System.Configuration 引用 dll。没有参考,以上都是没用的。