ServicePointManager.DefaultConnectionLimit返回Int32.MaxValue

时间:2016-04-22 11:26:21

标签: c# asp.net-mvc asp.net-web-api2

出于诊断目的,我正在记录ServicePointManager.DefaultConnectionLimit。但奇怪的是它似乎正在返回Int32.MaxValue(即2147483647)。

这似乎与该主题的MSDN documentation相矛盾:

  

ServicePoint对象允许的最大并发连接数。默认值为2.

对于上下文,我在运行于4.6.1

的ASP.Net 4应用程序中获取此值

1 个答案:

答案 0 :(得分:6)

Based on @Wimmel's link it seems in ASP.Net that it is set to Int32.MaxValue as part of the HTTP Runtime.

We can see this by looking inside the System.Web assembly at the HttpRuntime class.

There is a method called SetAutoConfigLimits which sets it to Int32.MaxValue. Here is the relevant excerpt:

private void SetAutoConfigLimits(ProcessModelSection pmConfig)
{
    int workerThreads;
    int completionPortThreads;
    ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
    if (pmConfig.DefaultMaxWorkerThreadsForAutoConfig != workerThreads || pmConfig.DefaultMaxIoThreadsForAutoConfig != completionPortThreads)
        UnsafeNativeMethods.SetClrThreadPoolLimits(pmConfig.DefaultMaxWorkerThreadsForAutoConfig, pmConfig.DefaultMaxIoThreadsForAutoConfig, true);
    ServicePointManager.DefaultConnectionLimit = int.MaxValue;
}