public static async Task<IPEndPoint> ToIPEndPointAsync (this Target self) { EndPoint endPoint = self.ToEndPoint(); IPEndPoint ipEndPoint = endPoint as IPEndPoint; if (ipEndPoint != null) return ipEndPoint; var dns = endPoint as DnsEndPoint; if (dns == null) throw new ArgumentException(); #if !WINDOWS_PHONE try { IPHostEntry entry = await Dns.GetHostEntryAsync (dns.Host).ConfigureAwait (false); return new IPEndPoint (entry.AddressList.First(), dns.Port); } catch (SocketException) { return null; } #else var helper = new DnsHelper(); IAsyncResult result = helper.BeginGetHostEntry (dns.Host, null, null); var addresses = await Task<IEnumerable<IPAddress>>.Factory.FromAsync (result, helper.EndGetHostEntry).ConfigureAwait (false); var address = addresses.FirstOrDefault(); if (address == null) return null; return new IPEndPoint (address, dns.Port); #endif }
public static async Task <IPEndPoint> ToIPEndPointAsync(this Target self) { EndPoint endPoint = self.ToEndPoint(); IPEndPoint ipEndPoint = endPoint as IPEndPoint; if (ipEndPoint != null) { return(ipEndPoint); } var dns = endPoint as DnsEndPoint; if (dns == null) { throw new ArgumentException(); } #if !WINDOWS_PHONE try { IPHostEntry entry = await Dns.GetHostEntryAsync(dns.Host).ConfigureAwait(false); return(new IPEndPoint(entry.AddressList.First(), dns.Port)); } catch (SocketException) { return(null); } #else var helper = new DnsHelper(); IAsyncResult result = helper.BeginGetHostEntry(dns.Host, null, null); var addresses = await Task <IEnumerable <IPAddress> > .Factory.FromAsync(result, helper.EndGetHostEntry).ConfigureAwait(false); var address = addresses.FirstOrDefault(); if (address == null) { return(null); } return(new IPEndPoint(address, dns.Port)); #endif }