41

I'm using the .NET WebRequest while changing my HOSTS file. I'm observing that System.Net doesn't honor those changes - how can I make it do so?

I have a number of servers load-balanced behind a single hostname, let's say 'example.com'. I want to target several of them individually, so my program will hard-code the machine-specific IP address in my HOSTS file before sending a request to example.com:

163.56.0.34  example.com

For the first server and first request, this works fine. Then my program changes the HOSTS file again:

163.56.0.48  example.com

And I create a new HttpWebRequest. When I send this one off, I can observe in NETMON that it goes to the first IP address (163.56.0.34) instead of the expected second one.

Using breakpoints and debug traces, I've verified that the correct value does get written to the HOSTS file each time. When I attempt to access example.com from a browser or other program, it does honor the HOSTS file and go to the second IP address.

Using NETMON I've verified that requests are going directly to the IP address shown; there is no HTTP proxy.

Since everything else is honoring the changed HOSTS file, I strongly suspect that the System.Net infrastructure has cached the DNS host-IP association for example.com. However, I can find no reference to this caching, and know of no way to flush it or turn it off.

I would welcome instructions for dealing with the cache, suggestions for what else might be causing these symptoms, or other proposed diagnostic steps that might be useful.

5 Answers 5

50

I finally dug up the obscure command from MSDN that fixes this:

ServicePointManager.DnsRefreshTimeout = 0;

As I unwound all the weird things I'd tried previously, I discovered one other setting that I need along with the one above; on the request object, turn off keep-alive:

request.KeepAlive = false;
2
  • 3
    Setting KeepAlive to false will result in a TCP handshake for every request. You can strike a middle ground by managing the ServicePoint.ConnectionLeaseTimeout so that you can reuse some connections, while still checking the DNS for possible changes on reconnect.
    – ScottS
    Commented Sep 11, 2017 at 20:13
  • for one who work with soap service using WCF ClientBase<T> instead of HttpWebRequest or WebClient stackoverflow.com/a/6535452/836376
    – KevinBui
    Commented Jan 13, 2021 at 4:25
15

A late answer to be sure, but I found this solution worked better than the accepted answer. In my scenario I am testing SQL connections, and I couldn't apply the existing answer since I have no request.KeepAlive to set.

This page by Brian Mancini ("derp turkey") details his adventure in clearing the DNS cache. Full props to him, I'm just adding his solution here:

public class DnsUtils  
{        
    [DllImport("dnsapi.dll", EntryPoint="DnsFlushResolverCache")]
    static extern UInt32 DnsFlushResolverCache();

    [DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_A")]
    public static extern int DnsFlushResolverCacheEntry(string hostName);

    public static void FlushCache()
    {
        DnsFlushResolverCache();
    }

    public static void FlushCache(string hostName)
    {
        DnsFlushResolverCacheEntry(hostName);
    }
}
2
  • Thanks so much mate!
    – wan_keen
    Commented Sep 25, 2018 at 9:46
  • wow this worked for me when I had lost all hope, thanks!
    – Daniele
    Commented Apr 11, 2022 at 10:49
5

If you want to keep the DnsRefreshTimeout > 0 then you can update the cache by making a call to:

System.Net.Dns.GetHostEntry("example.com");
1
  • Based on the Brian Mancini ("derp turkey") blog page referenced in Ian's answer, GetHostEntry() uses the cache. So this isn't a way to ensure the cache entry is cleared for that host name.
    – jk7
    Commented Mar 12, 2021 at 1:40
3

you could use System.Diagnostics.Process to launch ipconfig /flushdns

3
  • 7
    I tried that and it didn't work - it flushes the machine's DNS cache, but not System.Net's process-level cache.
    – Bruce
    Commented Sep 1, 2011 at 23:18
  • 2
    Kind of "you can restart the server" advice. Commented Jan 22, 2021 at 11:58
  • Haha - reinstall windows! Commented Jan 24, 2021 at 22:48
0

This was one of the top results for when I searched for '.net core flush dns of api endpoint' in Google as of March 2024.

After updating Route 53 to point the domain to a different public IP.

I had two servers running with the same WebAPI builds in IIS.

I left both servers running the 'World Wide Web Publishing Service' on Windows.

The clients, depending on which service internally, would hit the new server, sometimes the old server IP/domain.

I had to stop the 'World Wide Web Publishing Service' on the old server so it wouldn't respond to requests. After that, the .Net core service must've tried to get the new DNS and then it would communicate with the new server on the new IP/domain.

Long story short, if you're migrating to a different server, shut down the services in Windows so they will timeout, then internally .Net will seem to try to get the DNS entry again instead of just assuming all is well since the endpoint is still responding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.