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

Dre*_*mer 6 .net c# asp.net servicepoint

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

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

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

我的问题:

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

一些与此相关的有用链接:

在httpwebrequest中创建TCP连接的方式和位置,以及它与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();
                        }
                    }
                }
            }
        }
Run Code Online (Sandbox Code Playgroud)

问候,

小智 0

Int32.maxvalue 只是一个没有限制的占位符。您应该能够根据需要与自己建立尽可能多的联系。

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