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