当客户端连接到'localhost'上的服务时,为什么System.Net.ServicePoint.ConnectionLimit使用'7FFFFFFF'(Int32.MaxValue / 2147483647)?

时间:2014-04-08 07:35:14

标签: c# asp.net .net servicepoint

为什么System.Net.ServicePoint.ConnectionLimit使用' 7FFFFFFF' (Int32.MaxValue / 2147483647)当客户端连接到' localhost' 上的服务时,它决定使用' 2'如果服务在远程计算机上运行,​​则为默认值?

最初我认为如果没有设置servicepoint.connectionlimit,它将是ServicePointManager.DefaultConnectionLimit。但是,我刚刚意识到(一旦我从客户那里得到了一个问题),那就是它的Int32.MaxValue / 2147483647。

我做了一些研究(详情请参考下面的链接),但我无法找出它用于int32.maxvalue的原因。我可以猜测它可能是为了更好的性能,因为输入请求和响应消息不会越过边界。

我的问题:

  1. 为什么Int32.MaxValue服务是否在' localhost' 上运行? (我在英语中的任何解释;)我从反射器复制的代码片段也很棒 - 因为我猜想了这个意图 - 但是完全不了解代码:))
  2. 我理解它的性能 - 但来自' 2' (默认)为' int32.maxvalue'听起来很刺激。换句话说,只要请求不通过网络,为什么打开尽可能多的TCP连接是可以的。 (换句话说 - 为什么默认为int32.maxvalue - 没有副作用)
  3. 与此相关的一些有用链接:

    How and where the TCP connection has been created in httpwebrequest, and how is it related to servicepoint?

    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();
                            }
                        }
                    }
                }
            }
    

    此致

1 个答案:

答案 0 :(得分:1)

Int32.maxvalue只是一个没有限制的占位符。您应该能够根据需要创建尽可能多的连接。

您粘贴的代码基本上只是检查您是否连接到环回地址,如果是,则返回maxint,如果不是,则返回servicepoint.connectionlimit的值(默认情况下为2,但您可以更改它)