开发者

System.Runtime.Remoting.Channels.CoreChannel.GetMachineIP() from .NET Reflector - please explain

开发者 https://www.devze.com 2023-01-08 02:27 出处:网络
Using .Net Reflector on System.Runtime.Remoting.Channels.CoreChannel I decompiled the 2 methods below. GetMachineIp() is called when setting up an HttpChannel for remoting.

Using .Net Reflector on System.Runtime.Remoting.Channels.CoreChannel I decompiled the 2 methods below. GetMachineIp() is called when setting up an HttpChannel for remoting.

internal static string GetMachineIp()
{
    if (s_MachineIp == null)
    {
        IPHostEntry hostEntry = Dns.GetHostEntry(GetMachineName());
        AddressFamily addressFamily = Socket.SupportsIPv4 ? 
            AddressFa开发者_如何学运维mily.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;

}

My question is why would Dns.GetHostEntry() in GetMachineIP() fail with SocketException "No such host is known". GetMachineName() returns successfully (which also does a GetHostEntry). This is only happening on an isolated machine. Could it be something to do with incorrect DNS registration?


Check out the comments in this MSDN documentation:

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

It says that you need a forward host/A entry in the DNS for the machine. Another entry says that this has changed in version 4.0. So maybe you can try .NET Framework 4.0 and see if it solves your problem. If not check your DNS registration for the machine.

Hope that helps!


I probably encountered the same issue: Setting up a .NET Remoting HttpChannel failed with the "No such host is known" exception on an isolated machine.

The name of the machine turned out to be the cause. GetMachineName returned "12", which then got interpreted as an IP address by Dns.GetHostEntry. Changing the computer name resolved the issue.

You'll have the error for computer names which can be interpreted as IP addresses by IPAddress.Parse


This fixed the issue for me:

Add a mapping for the computer name to 127.0.0.1 in the hosts file (path: "c:\windows\system32\drivers\etc\hosts")

127.0.0.1 <computer name>
0

精彩评论

暂无评论...
验证码 换一张
取 消