【问题标题】:Error resolving name servers Xamarin.Forms Android with MongoDB使用 MongoDB 解析名称服务器 Xamarin.Forms Android 时出错
【发布时间】:2019-08-27 23:20:05
【问题描述】:

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

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

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

我正在使用 MongoDB 驱动程序 2.91

你能帮忙解决一下吗?

谢谢。

【问题讨论】:

  • 您说当您连接到 Mongo 并设置 android:targetSDKVersion="28" 时遇到问题,但我找到了一个针对 androidsdkversion 28 的示例,并构建项目,没有发现任何问题: devblogs.microsoft.com/xamarin/write-apps-using-mongodb-xamarin
  • 我之前看过这篇文章。该示例使用目标 androidsdkversion 27 和 MongoDriver 2.6.1。降级到这个版本的 MongoDB 驱动程序可能会破坏我的代码。

标签: mongodb xamarin.forms xamarin.android


【解决方案1】:

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

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

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

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

问题已经解决了。

【讨论】:

    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2013-02-13
    • 2016-05-21
    • 1970-01-01
    相关资源
    最近更新 更多