2

在具有 .net 框架 4.6.1 的 Visual Studio 2017 上的项目中使用 MongoDB.Driver (v2.9.1) 时,对集合运行聚合命令超时,但出现以下异常:

A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Direct", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/servername:27017" }", EndPoint: "Unspecified/servername:27017", State: "Disconnected", Type: "Unknown", LastUpdateTimestamp: "2019-09-04T08:15:29.3671785Z" }] }.

注意servername 对应运行 mongo 的服务器的实际 DNS 名称

使用相同的参数和完全相同的代码以及来自 nuget 的 MongoDB.Driver 版本,.net core 2.0 上不会发生超时

我在网上尝试了尝试不同连接模式(即replicaSet、直接、独立等)的建议,但在4.6.1上都没有,在.net core上都可以

我还启用了网络跟踪并在下面添加了一个片段

System.Net.Sockets Verbose: 0 : [23092] Entering Socket#33624151::Socket(AddressFamily#2)
System.Net.Sockets Verbose: 0 : [23092] Exiting Socket#33624151::Socket()
System.Net.Sockets Verbose: 0 : [23092] Entering Socket#33624151::BeginConnect(serverdnsname)
System.Net.Sockets Verbose: 0 : [23092] Entering Socket#33624151::InternalBeginConnectHostName(serverdnsname)
System.Net.Sockets Verbose: 0 : [23092] Entering DNS::UnsafeBeginGetHostAddresses(serverdnsname)
System.Net.Sockets Verbose: 0 : [18424] Entering DNS::GetHostByName(serverdnsname)
System.Net.Sockets Verbose: 0 : [23092] Exiting DNS::UnsafeBeginGetHostAddresses()     -> ResolveAsyncResult#34181910
System.Net.Sockets Verbose: 0 : [23092] Exiting Socket#33624151::InternalBeginConnectHostName()     -> MultipleAddressConnectAsyncResult#39201736
System.Net.Sockets Verbose: 0 : [23092] Exiting Socket#33624151::BeginConnect()     -> MultipleAddressConnectAsyncResult#39201736
System.Net.Sockets Verbose: 0 : [18424] Exiting DNS::GetHostByName()     -> IPHostEntry#63835064
System.Net.Sockets Verbose: 0 : [18424] Entering DNS::EndGetHostAddresses(ResolveAsyncResult#34181910)
System.Net.Sockets Verbose: 0 : [18424] Exiting DNS::EndGetHostAddresses()     -> IPHostEntry#63835064
System.Net.Sockets Verbose: 0 : [18424] Entering Socket#33624151::BeginConnectEx()
System.Net.Sockets Verbose: 0 : [18424] Entering Socket#33624151::InternalBind(0.0.0.0:0#0)
System.Net.Sockets Verbose: 0 : [18424] Exiting Socket#33624151::InternalBind()
System.Net.Sockets Verbose: 0 : [18424] Exiting Socket#33624151::BeginConnectEx()     -> ConnectOverlappedAsyncResult#11454272
System.Net.Sockets Verbose: 0 : [5944] Entering Socket#33624151::InternalEndConnect(ConnectOverlappedAsyncResult#11454272)
System.Net.Sockets Information: 0 : [5944] Socket#33624151 - Created connection from my0ip-address:15755 to server-ip-address:27017.
System.Net.Sockets Verbose: 0 : [5944] Exiting Socket#33624151::InternalEndConnect()
System.Net.Sockets Verbose: 0 : [9256] Entering Socket#33624151::Dispose()
Exception thrown: 'System.TimeoutException' in MongoDB.Driver.Core.dll

注意server-ip-address、serverdns name和my-ip-address都是占位符

请参阅下面的代码进行复制

var settings = new MongoClientSettings { 
  Server = new MongoServerAddress(serverName),
  ConnectionMode = ConnectionMode.Direct
  };
client = new MongoClient(settings);            
db = client.GetDatabase(dbName);
IMongoCollection<BsonDocument> agg1800 = db.GetCollection<BsonDocument>(<collectionName>);
...
var options = new AggregateOptions()
            {
                AllowDiskUse = true,
                BatchSize = 500,
                UseCursor = true
            };
var cursor = agg1800.Aggregate(pipeline, options); // this is where the timeout occurs
// pipeline is a custom pipeline for my query

我还通过 mongo 服务器 URL 实例化了 MongoClient,但我注意到来自 .net 4.6.1 和 .net core 2.0 的调用之间的一些客户端设置不同,因此我使用 MongoClientSettings 确保设置一致

当我从同一台机器上的 .net 核心程序尝试它时,我看到侦听器接受了来自我的 IP 地址的连接和元数据,见下文

2019-09-04T12:39:47.716+0000 I NETWORK  [listener] connection accepted from my-ip-address:14657 #23894 (92 connections now open)
2019-09-04T12:39:47.798+0000 I NETWORK  [conn23894] received client metadata from my-ip-address:14657 conn23894: { driver: { name: "mongo-csharp-driver", version: "2.8.1.0" }, os: { type: "Windows", name: "Microsoft Windows 10.0.17134", architecture: "x86_64", version: "10.0.17134" }, platform: ".NET Core 4.6.26328.01" }
2019-09-04T12:39:48.102+0000 I NETWORK  [listener] connection accepted from my-ip-address:47037 #23895 (93 connections now open)
2019-09-04T12:39:48.116+0000 I NETWORK  [conn23895] received client metadata from my-ip-address:47037 conn23895: { driver: { name: "mongo-csharp-driver", version: "2.8.1.0" }, os: { type: "Windows", name: "Microsoft Windows 10.0.17134", architecture: "x86_64", version: "10.0.17134" }, platform: ".NET Core 4.6.26328.01" }

使用 .net 4.6.1 代码,它接受了连接,但没有向我显示服务器端的元数据。

因此,这似乎不是身份验证问题、ip 地址/dns 问题或其他网络连接问题,或者配置错误的 mongodb 服务器

mongodbdriver 似乎没有正确连接到服务器,但我对这里可能出现的问题感到困惑。为什么 .net framework 4.6.1 和 .net core 2.0 之间的行为会有所不同?

关于如何进行的任何建议?我不想为 .net 核心重建我的整个项目,因为它做了很多其他事情,这两个框架之间存在已知的兼容性问题。帮助将不胜感激!

4

0 回答 0