在System.Runtime.Remoting.Channels.CoreChannel上使用.Net Reflector我反编译了下面的两个方法.在为远程处理设置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;
Run Code Online (Sandbox Code Playgroud)
}
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;
Run Code Online (Sandbox Code Playgroud)
}
我的问题是为什么GetMachineIP()中的Dns.GetHostEntry()会因SocketException而失败"没有这样的主机已知".GetMachineName()成功返回(也是GetHostEntry).这只发生在一台孤立的机器上.这可能与错误的DNS注册有关吗?
查看此 MSDN 文档中的注释:
http://msdn.microsoft.com/en-us/library/ms143998.aspx
它表示您需要该计算机的 DNS 中的转发主机/A 条目。另一篇文章说这在 4.0 版本中发生了变化。因此,也许您可以尝试 .NET Framework 4.0,看看它是否可以解决您的问题。如果没有,请检查您机器的 DNS 注册。
希望有帮助!