2017-08-23 19 views
1

我可以Dns.GetHostEntry(“google.com”)采取URI的IP地址,如“google.com”,例如:在.net核心的C# - Dns.GetHostEntry(uri.Host)?

 var ip = Dns.GetHostEntry("google.com"); 
     Console.WriteLine(ip.AddressList.FirstOrDefault().ToString()); 
     Console.ReadKey(); 

这个代码在.NET框架不错,但我有一个十字架平台项目,并需要在.net core 1.1和.net标准1.6中使用此代码,但.net核心和标准版本不支持System.Net.Dns.GetHostEntry, 如何从Dns中的主机名获取服务器IP地址.net核心和标准?

回答

2

检查后,我再次发现Dns.GetHostEntryAsync在.NET标准和.net的核心,我在我的代码更改为这个支持.NET4和.net45和.NET的核心和.net标准:

#if (NETSTANDARD1_6 || NETCOREAPP1_1) 
      IPHostEntry Host = await Dns.GetHostEntryAsync(uri.Host); 
#else 
      IPHostEntry Host = Dns.GetHostEntry(uri.Host); 
#endif 
+0

为什么NETSTANDARD1_6 || NETCOREAPP1_1'?而不只是'NETSTANDARD1_6'? 如果这是一个库,你只需要针对netstandard,而不是netcoreapp(或者我有兴趣知道为什么) – Treziac

+0

因为如果我将来在我的项目中使用Microsoft.NETCore.CoreCLR包.net standdard不支持库,我在我的项目中使用.net核心和.net标准,这只是后者 –