I'm connecting to mongodb instance and it all works fine :
async void Main()
{
string connection = "mongodb://....:27017/authdb?connectTimeoutMS=3000&socketTimeoutMS=3000";
MongoClient client = new MongoClient(connection);
Console.WriteLine(client.Settings.ConnectTimeout );
try
{
client.ListDatabaseNames(); //dummy operation
Console.WriteLine("done");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
output:
00:00:03
done
This means that the query string value is inserted to the client.Settings.ConnectTimeout
property.
But now I want to see if the timeouts I've declared in the query string are working. So I'm going to change to a wrong IP address.
When I change to a wrong IP , I get the following exception after 30 seconds :
A timeout occurred 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", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "....:27017" }", EndPoint: "10.153.14.188:27017", ReasonChanged: "Heartbeat", State: "Disconnected", ServerVersion: , TopologyVersion: , Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.TimeoutException: Timed out connecting to .....:27017. Timeout was 00:00:03. at MongoDB.Driver.Core.Connections.TcpStreamFactory.d__7.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Connections.TcpStreamFactory.d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext() --- End of inner exception stack trace --- at MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at MongoDB.Driver.Core.Servers.ServerMonitor.d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at MongoDB.Driver.Core.Servers.ServerMonitor.d__34.MoveNext()", LastHeartbeatTimestamp: "2021-07-12T13:39:27.6777575Z", LastUpdateTimestamp: "2021-07-12T13:39:27.6777575Z" }] }.
This happens after 30 seconds and not after 3 seconds as I declared in the query string.
Question
How can I make MongoClient
trying to connect and exit after 3 seconds, as declared in the query string ?