When using MongoDB.Driver (v2.9.1) in a project on visual studio 2017 with .net framework 4.6.1, running an aggregate command on a collection times out with the following exception:
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" }] }.
Note servername corresponds to the actual DNS name of the server running mongo
With the same parameters and exact same code and MongoDB.Driver version from nuget the time out does not occur on .net core 2.0
I tried the suggestions online for attempting different connection modes (i.e. replicaSet, direct, standalone etc.) but none work on 4.6.1 and all work on .net core
I also enabled network tracing and have added a snippet below
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
Note server-ip-address, serverdns name and my-ip-address are all placeholders
Please see the code below for the repro
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
I also instantiated the MongoClient via the mongo server URL but I noticed some client settings were different between the calls from .net 4.6.1 and .net core 2.0 so I used MongoClientSettings to make sure the settings were consistent
When I attempt it from the .net core program on the same machine i see that the listener accepted the connection and meta data from my ip address see below
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" }
with the .net 4.6.1 code it accepted the connection but did not show me the meta data on the server side.
So it seems that it isn't an auth problem, an ip address / dns problem or otherwise network connectivity issue, or a misconfigured mongodb server
it does seem that the mongodbdriver is not connecting to the server correctly but I'm stumped as to what might be the problem here. Why would there be a difference in behavior between .net framework 4.6.1 and .net core 2.0?
Any suggestions on how to proceed? I dont want to rebuild my entire project for .net core as it does a lot of other things for which there are known compat issues across the two frameworks. Help would be greatly appreciated!