Slow resolving + Multiple DNS servers


Resolving DNS records was terribly slow on my machine; the following fix solved it:
In DnsQueryRequest.cs change this (starting at line 204):
// Connect to DNS server and get the record for the current server.
IPHostEntry ipe = System.Net.Dns.GetHostEntry(dnsServer);
IPAddress ipa = ipe.AddressList[0];
IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);
To this:
// Connect to DNS server and get the record for the current server.
IPAddress ipa;
if (!IPAddress.TryParse(dnsServer, out ipa)) {
        IPHostEntry ipe = System.Net.Dns.GetHostEntry(dnsServer);
        ipa = ipe.AddressList[0];
IPEndPoint ipep = new IPEndPoint(ipa, (int)UdpServices.Domain);
Also, DnDns only tries the first (if any!) DNS server known to the system. If I may suggest some changes:
  • Add a public property DnsServers which is (or can be) populated in the Constructor with the systems DNS servers
  • The DnsServers could then be changed by the user to use, for example, Google's DNS servers ( and for example)
  • When an exception (or lookup failure or...) occurs, the next DnsServer (if more than one is specified) could be used
    I am willing to write proposed code/patch if you're interested.

file attachments


RobIII wrote Jul 20, 2011 at 12:42 PM

^ As a matter of fact, I just did :-) Changed files are attached. Short summary:

  • only changed for demo-purposes
  • DiscoverUnixDnsServerAddress has been refactored to DiscoverUnixDnsServerAddresses and returns a string array
  • DiscoverDnsServerAddresses now also returns a string array
  • Added a constructor which can be used to specify DNS servers (by name/IP) as string array
  • Added DnsServers property to DnsQueryRequest object
  • Added UseDefaultDnsServers() to reset the DnsServers to "default"
  • Resolve now incorporates above proposed "speedup" changes
  • Resolve now tries all specified DNS servers
  • When all DNS requests have failed an exception is thrown
Going through the code I noticed there's a lot of room for improvement; for example the Exceptions thrown should be DnsResolverExceptions or something similar as opposed to "generic" exceptions. Also there's a lot of unnessecary "ToString" stuff going on etc. etc. It's a shame I don't have much time available to chip in with this project.

jmonty wrote Jul 20, 2011 at 2:51 PM

RobIII ... thanks! You rock...I'll get these in the main code branch when i get some downtime.

softlion wrote Jan 30, 2012 at 12:00 PM

resolve could try all dns resolvers in // using Parrallel.For. This would get the fastest dns result.

wrote Feb 14, 2013 at 3:47 AM