C# mongo 驱动程序忽略连接超时?

Roy*_*mir 1 c# mongodb

我正在连接到 mongodb 实例,一切正常:

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

}
Run Code Online (Sandbox Code Playgroud)

输出:

00:00:03
done
Run Code Online (Sandbox Code Playgroud)

这意味着查询字符串值已插入到client.Settings.ConnectTimeout属性中。

但现在我想看看我在查询字符串中声明的超时是否有效。所以我要改一个错误的IP地址。

当我更改为错误的 IP 时,30 秒后出现以下异常:

A timeout occurred after 30000ms selecting a server using CompositeServerSelector{ Selectors =
Run Code Online (Sandbox Code Playgroud)

MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector,LatencyLimitingServerSelector{AllowedLatencyRange = 00:00:00.0150000 } }。集群状态的客户端视图为 { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "....:27017" }", EndPoint:“10.153.14.188:27017”,ReasonChanged:“Heartbeat”,状态:“已断开连接”,ServerVersion:,TopologyVersion:,类型:“未知”,HeartbeatException:“MongoDB.Driver.MongoConnectionException:打开连接时发生异常到服务器。 ---> System.TimeoutException:连接到 .....:27017 超时。超时为 00:00:03。 在 MongoDB.Driver.Core.Connections.TcpStreamFactory.d__7.MoveNext() -- - 从先前抛出异常的位置开始的堆栈跟踪结束 --- 在 MongoDB.Driver.Core.Connections 的 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 处的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处。 TcpStreamFactory.d__4.MoveNext() --- 从先前引发异常的位置开始的堆栈跟踪结束 --- 在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处,在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 处at MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext() --- 内部异常堆栈跟踪结束 --- at MongoDB.Driver.Core.Connections.BinaryConnection.d__51.MoveNext() --- 堆栈结束从先前引发异常的位置进行跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 MongoDB.Driver.Core.Servers.ServerMonitor.d__32 处的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处。 MoveNext() --- 从先前抛出异常的位置开始的堆栈跟踪结束 --- 在 MongoDB.Driver.Core.Servers.ServerMonitor.d__34.MoveNext() 的 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() 处", LastHeartbeatTimestamp:“2021-07-12T13:39:27.6777575Z”,LastUpdateTimestamp:“2021-07-12T13:39:27.6777575Z”}]}。

这发生在 30 秒后,而不是我在查询字符串中声明的 3 秒后。

问题

如何MongoClient按照查询字符串中的声明尝试连接并在 3 秒后退出?

Çöđ*_*xěŕ 5

如何让 MongoClient 尝试连接并在 3 秒后退出(如查询字符串中声明的那样)?

serverSelectionTimeoutMS您可以在连接字符串中添加来处理此问题。根据文档:

serverSelectionTimeoutMS - 指定在引发异常之前阻止服务器选择的时间(以毫秒为单位)。默认值:30,000 毫秒。

按照以下方式更新您的连接字符串:

 string connection = "mongodb://....:27017/authdb?serverSelectionTimeoutMS=3000&connectTimeoutMS=3000&socketTimeoutMS=3000";
Run Code Online (Sandbox Code Playgroud)

有关这些连接选项的更多信息,请参阅文档。