【问题标题】:System.Runtime.Remoting.Channels.CoreChannel.GetMachineIP() from .NET Reflector - please explainSystem.Runtime.Remoting.Channels.CoreChannel.GetMachineIP() 来自 .NET Reflector - 请解释
【发布时间】:2011-03-16 20:23:39
【问题描述】:

在 System.Runtime.Remoting.Channels.CoreChannel 上使用 .Net Reflector 我反编译了以下两种方法。 GetMachineIp() 在设置远程处理的 HttpChannel 时被调用。

internal static string GetMachineIp()
{
    if (s_MachineIp == null)
    {
        IPHostEntry hostEntry = Dns.GetHostEntry(GetMachineName());
        AddressFamily addressFamily = Socket.SupportsIPv4 ? 
            AddressFamily.InterNetwork : AddressFamily.InterNetworkV6;
        IPAddress machineAddress = GetMachineAddress(hostEntry, addressFamily);
        if (machineAddress != null)
        {
           s_MachineIp = machineAddress.ToString();
        }
        if (s_MachineIp == null)
        {
            throw new ArgumentNullException("ip");
        }
}
return s_MachineIp;

}

internal static string GetMachineName()
{
    if (s_MachineName == null)
    {
        string hostName = GetHostName();
        if (hostName != null)
        {
            IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
            if (hostEntry != null)
            {
                s_MachineName = hostEntry.HostName;
            }
        }
        if (s_MachineName == null)
        {
            throw new ArgumentNullException("machine");
        }
    }
    return s_MachineName;

}

我的问题是为什么 GetMachineIP() 中的 Dns.GetHostEntry() 会因 SocketException “No such host is known”而失败。 GetMachineName() 成功返回(也执行 GetHostEntry)。这只发生在孤立的机器上。会不会是因为 DNS 注册不正确?

【问题讨论】:

    标签: .net


    【解决方案1】:

    查看此 MSDN 文档中的 cmets:

    http://msdn.microsoft.com/en-us/library/ms143998.aspx

    它说您需要机器的 DNS 中的转发主机/A 条目。另一个条目说这在 4.0 版中已更改。所以也许你可以试试 .NET Framework 4.0 看看它是否能解决你的问题。如果不检查您的机器的 DNS 注册。

    希望有帮助!

    【讨论】:

      【解决方案2】:

      我可能遇到了同样的问题:在隔离的机器上设置 .NET Remoting HttpChannel 失败,出现“No such host is known”异常。

      原来是机器的名字造成的。 GetMachineName 返回“12”,然后被 Dns.GetHostEntry 解释为 IP 地址。更改计算机名称解决了这个问题。

      您将遇到计算机名称错误,IPAddress.Parse 可以将其解释为 IP 地址

      【讨论】:

        【解决方案3】:

        这为我解决了这个问题:

        在hosts文件中添加计算机名到127.0.0.1的映射(路径:“c:\windows\system32\drivers\etc\hosts”)

        127.0.0.1 <computer name>
        

        【讨论】:

          猜你喜欢
          • 2015-09-05
          • 2011-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多