System.Runtime.Remoting.Channels.CoreChannel.GetMachineIP() 来自 .NET Reflector - 请解释。

5

我使用 .Net Reflector 对 System.Runtime.Remoting.Channels.CoreChannel 进行反编译,得到了下面两个方法。在远程调用设置 HttpChannel 时会调用 GetMachineIp() 方法。

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注册不正确有关吗?

3个回答

1
我可能遇到了相同的问题:在隔离的机器上设置.NET Remoting HttpChannel时出现“找不到此主机”的异常。
机器名称是问题的原因。GetMachineName返回“12”,然后被Dns.GetHostEntry解释为IP地址。更改计算机名称解决了这个问题。
对于可以被IPAddress.Parse解释为IP地址的计算机名称,您将会看到错误。

1

请查看此 MSDN 文档中的评论:

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

它说您需要在DNS中为该计算机添加前向主机/ A 记录。另一个条目说明这在4.0版本中已更改。因此,您可以尝试使用.NET Framework 4.0并查看是否解决了您的问题。如果没有,请检查计算机的DNS注册。

希望能有所帮助!


0
这是解决我的问题的方法:
在hosts文件中(路径:“c:\windows\system32\drivers\etc\hosts”)添加计算机名称到127.0.0.1的映射。
127.0.0.1 <computer name>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接