Synchronous version of the method:
private static Boolean MongoDbConnection(String connectionString, String dbName) { var client = new MongoClient(connectionString); for (int k = 0; k < 6; k++) { client.GetDatabase(dbName); var server = client.Cluster .Description .Servers .FirstOrDefault(); if (server != null && server.HeartbeatException == null && server.State == ServerState.Connected) { break; } Thread.Sleep(300); } return false; }
Asynchronous version of the method:
private static async Task<Boolean> MongoDbConnectionAsync(String connectionString, String dbName) { return await Task.Run(() => { var client = new MongoClient(connectionString); for (int k = 0; k < 6; k++) { client.GetDatabase(dbName); var server = client.Cluster .Description .Servers .FirstOrDefault(); if (server != null && server.HeartbeatException == null && server.State == ServerState.Connected) { break; } Thread.Sleep(300); } return false; }); }
MongoClientdoes not say anything about this. But it is easy to check. Pass the obviously incorrect connection string and see what happens. And the client has aListDatabases()method, as I understand it, if the connection is successfully established, you can get a list of databases on the server. - Bulson