2010-07-16 53 views
5

在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註冊有關嗎?

回答

1

看看這個MSDN文檔中的評論:

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

它說,你需要一個前鋒主機/本機的DNS A項。另一個條目說這在4.0版本中已經改變。所以,也許你可以嘗試.NET Framework 4.0,看看它是否能解決你的問題。如果沒有檢查您的機器的DNS註冊。

希望有幫助!

1

我可能遇到了同樣的問題:設置.NET Remoting HttpChannel失敗,在隔離的機器上出現「No such host is known」異常。

機器的名字竟然是原因。 GetMachineName返回「12」,然後通過Dns.GetHostEntry將其解釋爲IP地址。更改計算機名稱可解決問題。

你有可以通過IPAddress.Parse

被解釋爲IP地址
0

這個固定我的問題的計算機名稱錯誤:

添加映射爲計算機名稱爲127.0.0.1在hosts文件中(路徑:「c:\ windows \ system32 \ drivers \ etc \ hosts」)

127.0.0.1 <computer name>