Search code examples
mongodbxamarin.formsxamarin.android

Error resolving name servers Xamarin.Forms Android with MongoDB


I have the below issue connecting to Mongo Atlas when I set android:targetSDKVersion="28" in my android manifest.

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.AggregateException: Error resolving name servers ---> System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Where[TSource] (System.Collections.Generic.IEnumerable1[T] source, System.Func2[T,TResult] predicate) [0x0000d] in <715c2ff6913942e6aa8535593b3ef35a>:0 at DnsClient.NameServer.QueryNetworkInterfaces (System.Boolean skipIPv6SiteLocal) [0x00047] in <93b57b4b99c64a96a2c065ea9ae1fc1f>:0 at DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x0000d] in <93b57b4b99c64a96a2c065ea9ae1fc1f>:0 --- End of inner exception stack trace --- at DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x0005e] in <93b57b4b99c64a96a2c065ea9ae1fc1f>:0 at DnsClient.LookupClient..ctor () [0x00000] in <93b57b4b99c64a96a2c065ea9ae1fc1f>:0 at MongoDB.Driver.Core.Configuration.ConnectionString.Resolve (System.Boolean resolveHosts) [0x00011] in <861d33dc90734b91874371b41764f591>:0 at MongoDB.Driver.MongoUrl.Resolve (System.Boolean resolveHosts) [0x00015] in :0 at MongoDB.Driver.MongoClientSettings.FromUrl (MongoDB.Driver.MongoUrl url) [0x0001b] in :0 at MongoDB.Driver.MongoClientSettings.FromConnectionString (System.String connectionString) [0x00006] in :0 at MongoDB.Driver.MongoClient..ctor (System.String connectionString) [0x00000] in :0

If I remove the android:targetSDKVersion="28", the app connects without the issue but google play requires that I set the targetVersion before I can upload to play store.

I am using MongoDB Driver 2.91

Can you please help with possibly the solution.

Thanks.


Solution

  • I found out that from Android 8 (Oreo), access to net.dns has been removed. I was connecting to the server with SRV lookup according to the connection string from MongoDB 2.9 Driver. The DNSClient was Performing a DNS lookup on the domain name on the connection string. The below issue from DNSClient.net library shows more.

    https://github.com/MichaCo/DnsClient.NET/issues/17

    However, I had to do a custom implementation to get DNS Servers using the below code:

    public List<IPEndPoint> GetDnsServers()
    {
        var context = Android.App.Application.Context;
        List<IPEndPoint> endPoints = new List<IPEndPoint>();
        ConnectivityManager connectivityManager = 
    (ConnectivityManager)context.GetSystemService(MainActivity.ConnectivityService);
    
    Network activeConnection = connectivityManager.ActiveNetwork;
    var linkProperties = connectivityManager.GetLinkProperties(activeConnection);
    
    foreach (InetAddress currentAddress in linkProperties.DnsServers)
    {
        IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(currentAddress.HostAddress), 53);
        endPoints.Add(endPoint);
    }
    
    return endPoints;
    }
    

    The issue is gone.