Bug 48016 - System.Net.NetworkInformation.DnsAddresses is always empty. Fix included.
Summary: System.Net.NetworkInformation.DnsAddresses is always empty. Fix included.
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 7.1 (C9)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Habersack
Depends on:
Reported: 2016-11-26 11:40 UTC by softlion
Modified: 2017-01-12 22:58 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description softlion 2016-11-26 11:40:11 UTC
// Decompiled with JetBrains decompiler
// Type: System.Net.NetworkInformation.UnixIPInterfaceProperties
// Assembly: System, Version=, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
// MVID: 9D8F5F10-3A5E-4A27-A4FD-F703CE37CAE4
// Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.dll

This buggy method always returns an empty collection for any interface.

  public override IPAddressCollection DnsAddresses
        return this.dns_servers;

This is because resolv.conf is not available on android systems. Instead you have to get the system properties: net.dns1, net.dns2 ... net.dns8

        public static IEnumerable<IPAddress> GetDnsServers()
            var dnsEntries = new List<string>();
                var systemPropertiesClass = Java.Lang.Class.ForName("android.os.SystemProperties");
                var getMethod = systemPropertiesClass.GetMethod("get", Java.Lang.Class.FromType(typeof(Java.Lang.String)));
                for (var i = 1; i < 8; i++)
                    var value = ((Java.Lang.String)getMethod.Invoke(null, $"net.dns{i}"))?.ToString();
                    if (!String.IsNullOrEmpty(value) && !dnsEntries.Contains(value))
            catch (Java.Lang.Exception e)

            return dnsEntries.Select(entry => IPAddress.Parse(entry)).ToList();
Comment 2 softlion 2016-11-26 11:56:42 UTC
Well i can't as it is not Xamarin Android.
Comment 3 Marek Habersack 2016-11-28 17:35:03 UTC
@softlion you can PR against mono/master, the code will eventually end up in XA - we use the standard Mono BCL. However, since you need to use Java classes, the code should live in Xamarin.Android and, preferably, should be done in C to directly use Java JNI without involving managed code (saves memory and time). See for instance here https://github.com/xamarin/xamarin-android/blob/master/src/monodroid/jni/monodroid-networkinfo.c#L113 and here https://github.com/mono/mono/blob/master/mcs/class/System/System.Net.NetworkInformation/NetworkInterface.cs#L634

Note how the C code caches information to save time.

You would need, therefore, two PRs - first for XA and after that one is accepted, another one for Mono (we don't want to introduce changes to Mono that will cause runtime failure because the XA part is missing)
Comment 4 Marek Habersack 2017-01-11 10:45:17 UTC
A PR for Xamarin.Android is opened https://github.com/xamarin/xamarin-android/pull/360
Comment 5 Marek Habersack 2017-01-12 22:58:15 UTC
Fixed in:

   mono/master, commit bb9981e61c009589fb16aeb9287cbb17f26db533
   mono/mono-4.8.0-branch, commit 28d9c96aec01023da0857d44b21e6032f7a2b81f
   xamarin-android/master, commit 5d6d4126513f8ce83ad09d394ad43ea36c94b5c3
   monodroid/master, commit 32da15e6ef8617bde182e92b6de42deaeb7e824c