【发布时间】: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