为什么System.Net.ServicePoint.ConnectionLimit使用' 7FFFFFFF' (Int32.MaxValue / 2147483647)当客户端连接到' localhost' 上的服务时,它决定使用' 2'如果服务在远程计算机上运行,则为默认值?
最初我认为如果没有设置servicepoint.connectionlimit,它将是ServicePointManager.DefaultConnectionLimit。但是,我刚刚意识到(一旦我从客户那里得到了一个问题),那就是它的Int32.MaxValue / 2147483647。
我做了一些研究(详情请参考下面的链接),但我无法找出它用于int32.maxvalue的原因。我可以猜测它可能是为了更好的性能,因为输入请求和响应消息不会越过边界。
我的问题:
与此相关的一些有用链接:
http://blogs.microsoft.co.il/idof/2011/06/20/servicepointmanagerdefaultconnectionlimit-2-depends/
http://msdn.microsoft.com/en-us/library/system.net.servicepoint.connectionlimit(v=vs.110).aspx
http://arnosoftwaredev.blogspot.com/2006/09/net-20-httpwebrequestkeepalive-and.html
来自Reflector的代码段
public int ConnectionLimit
{
get
{
if ((!this.m_UserChangedLimit && (this.m_IPAddressInfoList == null)) && (this.m_HostLoopbackGuess == TriState.Unspecified))
{
lock (this)
{
if ((!this.m_UserChangedLimit && (this.m_IPAddressInfoList == null)) && (this.m_HostLoopbackGuess == TriState.Unspecified))
{
IPAddress address = null;
if (IPAddress.TryParse(this.m_Host, out address))
{
this.m_HostLoopbackGuess = IsAddressListLoopback(new IPAddress[] { address }) ? TriState.True : TriState.False;
}
else
{
this.m_HostLoopbackGuess = NclUtilities.GuessWhetherHostIsLoopback(this.m_Host) ? TriState.True : TriState.False;
}
}
}
}
if (!this.m_UserChangedLimit && !((this.m_IPAddressInfoList == null) ? (this.m_HostLoopbackGuess != TriState.True) : !this.m_IPAddressesAreLoopback))
{
return 0x7fffffff;
}
return this.m_ConnectionLimit;
}
set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException("value");
}
if (!this.m_UserChangedLimit || (this.m_ConnectionLimit != value))
{
lock (this)
{
if (!this.m_UserChangedLimit || (this.m_ConnectionLimit != value))
{
this.m_ConnectionLimit = value;
this.m_UserChangedLimit = true;
this.ResolveConnectionLimit();
}
}
}
}
}
此致
答案 0 :(得分:1)
Int32.maxvalue只是一个没有限制的占位符。您应该能够根据需要创建尽可能多的连接。
您粘贴的代码基本上只是检查您是否连接到环回地址,如果是,则返回maxint,如果不是,则返回servicepoint.connectionlimit的值(默认情况下为2,但您可以更改它)