首页 > 解决方案 > C# mongo db驱动程序随机无法连接到db

问题描述

我有控制台应用程序,它通过 C# mongo 驱动程序在单独的线程中调用 mongo db。连接池限制为10,连接字符串为:mongodb+srv://login:pass@address/test?retryWrites=true&w=majority&connect=replicaSet

在我启动控制台应用程序的一半时间里,它会在 30 秒超时后下降。当我重新启动应用程序时,它会以 50% 的概率正常工作。如果第一次操作失败 - 任何其他操作也会失败。如果第一个没问题,其他任何一个都可以正常工作。

例外:

MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [], DnsMonitorException : "DnsClient.DnsResponseException: Unhandled exception ---> System .IndexOutOfRangeException:无法读取字节 277,超出范围。在 DnsClient.DnsDatagramReader.ReadLabels() 在 DnsClient.DnsDatagramReader.ReadQuestionQueryString() 在 DnsClient.DnsRecordFactory.ReadRecordInfo() 在 DnsClient.DnsMessageHandler.GetResponseMessage (数组段1 responseData) at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request, TimeSpan timeout) at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection1 个服务器,DnsMessageHandler 处理程序,DnsRequestMessage 请求,Boolean useCache,LookupClientAudit continueAudit) --- 内部异常堆栈跟踪结束 --- 在 DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection1 个服务器,DnsQuestion 问题,Boolean useCache) 在 DnsClient.LookupClient.Query (String query, QueryType queryType, QueryClass queryClass) at MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveSrvRecords(String service, CancellationToken cancelToken) at MongoDB.Driver.Core.Clusters.DnsMonitor.Monitor()" }.)'

此异常最初是在此调用堆栈中引发的:MongoDB.Driver.Core.Clusters.Cluster.ThrowTimeoutException(MongoDB.Driver.Core.Clusters.ServerSelectors.IServerSelector, MongoDB.Driver.Core.Clusters.ClusterDescription) MongoDB.Driver.Core。 Clusters.Cluster.WaitForDescriptionChangedHelper.HandleCompletedTask(System.Threading.Tasks.Task)MongoDB.Driver.Core.Clusters.Cluster.WaitForDescriptionChangedAsync(MongoDB.Driver.Core.Clusters.ServerSelectors.IServerSelector,MongoDB.Driver.Core.Clusters.ClusterDescription, System.Threading.Tasks.Task, System.TimeSpan, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) MongoDB.Driver .Core.Clusters.Cluster.SelectServerAsync(MongoDB。Driver.Core.Clusters.ServerSelectors.IServerSelector, System.Threading.CancellationToken) System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task) System.Runtime.CompilerServices .TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task) ... [调用堆栈被截断]

内:

使用 CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } } 选择服务器 30000 毫秒后发生超时。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode : "ReplicaSet", Type : "ReplicaSet", State : "Disconnected", Servers : [], DnsMonitorException : "DnsClient.DnsResponseException: Unhandled exception ---> System .IndexOutOfRangeException: 无法读取字节 277,超出范围。在 DnsClient.DnsDatagramReader.ReadLabels() 在 DnsClient.DnsRecordFactory.ReadRecordInfo() 在 DnsClient.DnsRecordFactory.ReadRecordInfo() 在 DnsClient.DnsDatagramReader.ReadLabels() 在 DnsClient.DnsRecordFactory.ReadRecordInfo() 读取字节 277。1 responseData) at DnsClient.DnsUdpMessageHandler.Query(IPEndPoint server, DnsRequestMessage request, TimeSpan timeout) at DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection1 个服务器,DnsMessageHandler 处理程序,DnsRequestMessage 请求,Boolean useCache,LookupClientAudit continueAudit) --- 内部异常堆栈跟踪结束 --- 在 DnsClient.LookupClient.ResolveQuery(IReadOnlyCollection 1 servers, DnsMessageHandler handler, DnsRequestMessage request, Boolean useCache, LookupClientAudit continueAudit) at DnsClient.LookupClient.QueryInternal(IReadOnlyCollection1 个服务器,DnsQuestion 问题,Boolean useCache) 在 DnsClient.LookupClient.Query (字符串查询,QueryType queryType,QueryClass queryClass)在 MongoDB.Driver.Core.Misc.DnsClientWrapper.ResolveSrvRecords(字符串服务,CancellationToken cancelToken)在 MongoDB.Driver.Core.Clusters.DnsMonitor.Monitor()"}。

我问了我们的 NodeJS 开发人员,他回答说他没有这样的问题。有人遇到这个问题吗?

标签: c#mongodb

解决方案


I was also getting the exact error for last one day. I am not sure the exact problem but it is definitely related to what network I use. I tested by connecting my dev machine to my mobile's hot-spot and it worked. But when I again switched to broadband connection and it fails.

Hope it helps someone.

I have also raised a bug on mongodb support. Here is the link.

EDIT:

Finally got it fixed by upgrading the "DnsClient" nuget package to version 1.3.0.


推荐阅读