Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET caches DNS forever in Server 2008

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?

like image 584
reustmd Avatar asked Mar 16 '13 05:03

reustmd


3 Answers

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.

like image 168
Jawad Al Shaikh Avatar answered Nov 13 '22 01:11

Jawad Al Shaikh


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.

like image 43
Only You Avatar answered Nov 13 '22 02:11

Only You


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

like image 31
Nirav Avatar answered Nov 13 '22 03:11

Nirav