private static void PutEntry(IPAddress ip, CachedIPHostEntry entry) { lock (CachedIPHostEntryDict) { if (CachedIPHostEntryDict.ContainsKey(ip)) { CachedIPHostEntryDict[ip] = entry; LogHelper.Debug($"End resolve IPHostEntry for {ip}: IsResolved={entry.IsResolved}, HasErrors={entry.HasErrors}, ToolTipText={entry.DisplayText}"); } else { CachedIPHostEntryDict.Add(ip, entry); LogHelper.Debug($"Start resolve IPHostEntry for {ip}: IsResolved={entry.IsResolved}, HasErrors={entry.HasErrors}, ToolTipText={entry.DisplayText}"); } } }
public static async Task <CachedIPHostEntry> ResolveIpAddress(string ip) { return(await Task.Run(() => { if (!IsIPValid(ip)) { return CachedIPHostEntry.EMTPY; } IPAddress ipa = null; try { ipa = IPAddress.Parse(ip); ResolveIP(ipa); return CachedIPHostEntryDict[ipa]; } catch (Exception e) { return CachedIPHostEntry.ERROR_ENTRY(ipa, e); } }).ConfigureAwait(false)); }
private static void ResolveIP(IPAddress ip) { try { if (!CachedIPHostEntryDict.ContainsKey(ip)) { PutEntry(ip, CachedIPHostEntry.EMTPY); // reserve slot // http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/Net/System/Net/DNS@cs/1305376/DNS@cs IPHostEntry resolvedEntry = Dns.GetHostEntry(address: ip); CachedIPHostEntry entry = new CachedIPHostEntry { HostEntry = resolvedEntry, IsResolved = true, DisplayText = resolvedEntry.HostName }; PutEntry(ip, entry); } } catch (Exception e) { PutEntry(ip, CachedIPHostEntry.ERROR_ENTRY(ip, e)); } }