3

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:

  • $ ping myurl.local => 192.168.1.20

  • $ nslookup myurl.local => 192.168.1.20

  • Log from my code: Dns.GetHostAddresses("myurl.local")[0].ToString() => 192.168.1.101

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.

Community
  • 1
  • 1
Mehran
  • 12,150
  • 14
  • 78
  • 191
  • did you manage to resolve this issue? I seem to be having a similar problem with mono... – David Duffett Aug 10 '15 at 05:00
  • @DavidDuffett As you can see it's been a while since I had this problem. Back then I couldn't fix it and had to use a new DNS! – Mehran Aug 10 '15 at 07:46

3 Answers3

1

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!

0

Unfortunately its solution does not apply to mine, since ServicePointManager.DnsRefreshTimeout is not implemented in Mono!

Welcome to the open source world :) If you don't like something or want to improve, please send a pull request.

knocte
  • 14,579
  • 6
  • 67
  • 110
  • 1
    I think you are misunderstood. Ping resolves to the new IP address, not the old one. – Mehran Jan 12 '14 at 15:07
  • then you wrote misleading text in the question, see `ping myurl.local => 192.168.1.20` which is the old IP – knocte Jan 12 '14 at 19:44
  • 1
    Misunderstanding again, 192.168.1.20 is the new IP! – Mehran Jan 12 '14 at 20:13
  • 1
    Thanks, the pull request has already been made by @geogrepiva in the other post. Do you think I should send another one? – Mehran Jan 13 '14 at 06:21
  • seems that somebody followed my advice and contributed a fix: https://github.com/mono/mono/commit/932359f3d627da13408350b1172ceb63c30f6327 – knocte Dec 29 '15 at 11:02
0

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/");
Elad Nava
  • 6,524
  • 2
  • 36
  • 59
  • It seems there's been a misunderstanding, the problem is that Mono resolves the domain to an old IP address. It seems the IP address for some domain is cached somewhere and even though the domain points to some other IP now, but Mono still resolves it to the same old IP address. So your solution will face the same fate as the stem, with incorrect IP. – Mehran Aug 13 '15 at 07:49
  • Are you sure? Mono caches the old IP but only internally, when it is resolved via HttpWebRequest. Not via Dns.GetHostEntry. That DNS request does not get cached. – Elad Nava Aug 13 '15 at 09:41