Mehran
Mehran

Reputation: 16821

Resetting DNS cache in C# + Mono

I'm developing a software with C# + Mono in Ubuntu which makes use of network classes like WebRequest or Dns. During development phase, I used my code to connect to a local webserver 192.168.1.101 and after a while I had to move it to 102.168.1.20, and I used a local DNS server giving the mentioned IPs readable names (to simulate the real world scenario). But now, no matter what I do, I can not connect to the new server in my C# code! Here are things as they are:

It seems to me that Mono has cached the DNS' resolved results from before and it won't let go of them. So I searched the Internet for it and I found this question. It has the same problem as mine but in Windows and .Net and also a solution. Unfortunately, its solution does not apply to mine, since ServicePointManager.DnsRefreshTimeout is not implemented in Mono! The worst part is that the cache is persistent even if I restart the software and/or hardware thus I'm unable to proceed with my development!

So I wonder if there is a way I can reset Mono's cache, other than using DnsRefreshTimeout? Even a manual solution will do for now (something I can do in shell!? Like removing a file perhaps!?).

I'm using Ubuntu 12.04 and Mono 3.2.3.

Upvotes: 3

Views: 2499

Answers (2)

Brett Christensen
Brett Christensen

Reputation: 151

Good news everyone, the latest mono alpha version (mono 4.3.2, available here: http://www.mono-project.com/download/alpha/ ) now supports ServicePointManager.DnsRefreshTimeout!

Upvotes: 1

Elad Nava
Elad Nava

Reputation: 7906

As a workaround, request the DNS manually and connect directly to the IP, for example:

// Get hostname IPs manually (this may fail)
IPAddress[] ips = Dns.GetHostEntry('api.example.com').AddressList;

// New web request, initialize with endpoint URL
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://" + ips[0].ToString() + "/api/");

Upvotes: 0

Related Questions