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?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With