在System.Runtime.Remoting.Channels.CoreChannel上使用.Net反射器我反編譯了下面的兩個方法。爲遠程處理設置HttpChannel時調用GetMachineIp()。.NET反射器的System.Runtime.Remoting.Channels.CoreChannel.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;
}
我的問題是,爲什麼會Dns.GetHostEntry()在GetMachineIP()失敗,SocketException 「沒有這樣的主機被稱爲」。 GetMachineName()成功返回(也會執行GetHostEntry)。這隻發生在孤立的機器上。這可能是與不正確的DNS註冊有關嗎?