使用 MongoDB 解析名称服务器 Xamarin.Forms Android 时出错 [英] Error resolving name servers Xamarin.Forms Android with MongoDB

查看:22
本文介绍了使用 MongoDB 解析名称服务器 Xamarin.Forms Android 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在我的 android manifest 中设置 android:targetSDKVersion="28" 时,我在连接到 Mongo Atlas 时遇到了以下问题.

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

System.Reflection.TargetInvocationException:调用目标抛出异常.---> System.AggregateException:解析名称服务器时出错---> System.ArgumentNullException:值不能为空.参数名称:来源在 System.Linq.Enumerable.Where[TSource] (System.Collections.Generic.IEnumerable1[T] source, System.Func2[T,TResult] 谓词) [0x0000d] 在 <715c2ff6913942e6aa8535593b3>:0在 DnsClient.NameServer.QueryNetworkInterfaces (System.Boolean skipIPv6SiteLocal) [0x00047] 在 <93b57b4b99c64a96a2c065ea9ae1fc1f>:0在 DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x0000d] 在 <93b57b4b99c64a96a2c065ea9ae1fc1f>:0--- 内部异常堆栈跟踪结束 ---在 DnsClient.NameServer.ResolveNameServers (System.Boolean skipIPv6SiteLocal, System.Boolean fallbackToGooglePublicDns) [0x0005e] 在 <93b57b4b99c64a96a2c065ea9ae1fc1f>:0在 DnsClient.LookupClient..ctor () [0x00000] 在 <93b57b4b99c64a96a2c065ea9ae1fc1f>:0在 MongoDB.Driver.Core.Configuration.ConnectionString.Resolve (System.Boolean resolveHosts) [0x00011] 在 <861d33dc90734b91874371b41764f591>:0在 MongoDB.Driver.MongoUrl.Resolve (System.Boolean resolveHosts) [0x00015] 中:0在 MongoDB.Driver.MongoClientSettings.FromUrl (MongoDB.Driver.MongoUrl url) [0x0001b] 中:0在 MongoDB.Driver.MongoClientSettings.FromConnectionString (System.String connectionString) [0x00006] 中:0在 MongoDB.Driver.MongoClient..ctor (System.String connectionString) [0x00000] 中:0

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

如果我删除 android:targetSDKVersion="28",应用程序连接没有问题,但 google play 要求我在上传到 play 商店之前设置 targetVersion.

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.

我使用的是 MongoDB 驱动程序 2.91

I am using MongoDB Driver 2.91

能否请您帮忙解决可能的问题.

Can you please help with possibly the solution.

谢谢.

推荐答案

我发现从 Android 8 (Oreo) 开始,对 net.dns 的访问已被删除.我根据来自 MongoDB 2.9 驱动程序的连接字符串使用 SRV 查找连接到服务器.DNSClient 正在对连接字符串上的域名执行 DNS 查找.DNSClient.net 库中的以下问题显示了更多信息.

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

但是,我必须进行自定义实现才能使用以下代码获取 DNS 服务器:

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;
}

问题消失了.

这篇关于使用 MongoDB 解析名称服务器 Xamarin.Forms Android 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆