7

I'm having an issue where my .NET web requests are always hitting the same IP for a given CNAME. The CNAME is bound to multiple IP addresses, and those IP addresses are returned in a random order each time a DNS lookup occurs (confirmed via running nslookup).

I wrote a simple app to test this:

    public void TestDNS()
    {
        while (true)
        {
            var ipAddress = Dns.GetHostAddresses("mywebsite.com").First().ToString();

            Console.WriteLine(string.Format("Current: '{0}' at: '{1}'", ipAddress, DateTime.Now.ToLongTimeString()));

            Thread.Sleep(1000);
        }
    }

I have run this app on Server 2008 Datacenter, Server 2008 R2 Datacenter, Server 2012, Windows 7, and Windows 8. All machines had the latest updates and service packs available.

The only machine that fails is Server 2008. Every other machine switches the IP address about once a minute (the TTL is 1 minute). Server 2008 never switches (ran for over an hour).

To be clear, running nslookup and other tools on the machine seems to indicate that DNS is returning exactly what I'd expect. This issue seems to be specific to .NET on Server 2008.

I have tried the items listed in this related answer with no success.

Any ideas?

Community
  • 1
  • 1
reustmd
  • 3,513
  • 5
  • 30
  • 41
  • Can you have the DNS server return a completely different set of IP addresses for each call? That will tell you if the items are being cached, or if for some reason the runtime is just sorting the values that are returned. – Jim Mischel Mar 18 '13 at 15:17
  • Good idea @JimMischel. I tried changing the DNS record after the .NET application was running and nothing changed. It doesn't seem to pick up any DNS changes (i.e., cached forever). The only thing I've found that help is updating the hosts file. If I do that, it updates the DNS within the running .NET application. – reustmd Mar 18 '13 at 22:11
  • Method GetHostAddresses() returns an array of type IPAddress. See my answer. – Only You Mar 25 '13 at 14:04
  • I'm seeing a similar problem. I know this is an old question, but were the servers hosted in the cloud (if so which one)? – TheCloudlessSky Dec 10 '15 at 15:24
  • @TheCloudlessSky these servers were on AWS. I did not attempt replicating this issue with my own hardware or another cloud provider. – reustmd Dec 11 '15 at 18:49
  • @manu08 Interesting, ours are hosted on AWS too. Thanks! – TheCloudlessSky Dec 13 '15 at 23:45
  • 1
    @TheCloudlessSky good luck. in-case you missed it below, I used this app to workaround the issue on our servers: https://github.com/reustmd/HostFileUpdater – reustmd Dec 15 '15 at 00:34
  • @manu08 Thanks! Right now, we're using PInvoke via http://stackoverflow.com/a/16821805/200322 when an exception occurs and it *seems* to fix the problem `¯\_(ツ)_/¯` – TheCloudlessSky Dec 21 '15 at 01:39

4 Answers4

3

as workaround, is ping report same issue?

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

if you would accept workarounds, another possible solution would be searching for 3rd party library/code that resolve hostname Or invoke command line through [System.Diagnostics.Process();].

hope that's help.

Jawad Al Shaikh
  • 2,475
  • 2
  • 28
  • 41
  • Thanks. The workaround I've done right now is invoking wslookup via a .NET process command and updating the hosts file. It's a hack, but it is simple, and it works. Ultimately I'll be upgrading to Server 2012, which doesn't have this issue. – reustmd Mar 24 '13 at 16:22
  • Here's the code, it basically takes a target url, and will assign the IP to as many hostnames in the hosts file as you want. https://github.com/reustmd/HostFileUpdater/tree/master/HostFileUpdater – reustmd Mar 25 '13 at 13:31
  • Method GetHostAddresses() returns an array of type IPAddress; see my answer. – Only You Mar 25 '13 at 13:52
3

Method GetHostAddresses() returns an array of type IPAddress.

Return Value

Type: System.Net.IPAddress[]

An array of type IPAddress that holds the IP addresses for the host that is specified by the hostNameOrAddress parameter.

Change your var to an IPAddress array. Here is the example:

public static void DoGetHostAddresses(string hostname)
{
    IPAddress[] ips;

    ips = Dns.GetHostAddresses(hostname);

    Console.WriteLine("GetHostAddresses({0}) returns:", hostname);

    foreach (IPAddress ip in ips)
    {
        Console.WriteLine("    {0}", ip);
    }
}

See this page for more details.

Only You
  • 2,051
  • 1
  • 21
  • 34
1

You are making recursive queries. If you don't want the DNS server to cache any recursive queries create new DWORD "MaxCacheTTL" with value 0x0 at

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\DNS\Parameters

Nirav
  • 187
  • 3
  • 11
0

Are you running the same .Net environment on all machines, i.e. your code is compiled to the same target (x86 or x64 ) and same .Net framework 2.0 or 3.5 or 4.0

Ibrahim Magdy
  • 343
  • 1
  • 3
  • 14
  • Yes. I copy-and-pasted exe and DLLs to the different machines. Every OS was 64-bit. – reustmd Mar 25 '13 at 13:29
  • 1
    I assume this doesn't mean that all have the same framework http://stackoverflow.com/questions/7084028/can-net-2-0-assemblies-run-under-net-4-0 – Ibrahim Magdy Mar 25 '13 at 13:56
  • either way I think that this is a bug in .Net framework of framework integration, that should be reported to Microsoft but they don't have a bug reporting portal :( – Ibrahim Magdy Mar 25 '13 at 13:58