【问题标题】:ServicePointManager.DefaultConnectionLimit returning Int32.MaxValueServicePointManager.DefaultConnectionLimit 返回 Int32.MaxValue
【发布时间】:2016-04-22 11:26:20
【问题描述】:
出于诊断目的,我正在记录 ServicePointManager.DefaultConnectionLimit。然而奇怪的是,它似乎返回了 Int32.MaxValue(即 2147483647)。
这似乎与MSDN documentation 的主题相矛盾:
ServicePoint 对象允许的最大并发连接数。默认值为 2。
对于上下文,我在 4.6.1 上运行的 ASP.Net 4 应用程序中获取此值
【问题讨论】:
标签:
c#
asp.net-mvc
asp.net-web-api2
【解决方案1】:
根据@Wimmel 的链接,似乎在 ASP.Net 中它被设置为Int32.MaxValue 作为 HTTP 运行时的一部分。
我们可以通过looking inside the System.Web assembly at the HttpRuntime class看到这个。
有一个名为SetAutoConfigLimits 的方法将其设置为Int32.MaxValue。
以下是相关摘录:
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;
}