How do you call the Ping
command with the new C# driver 2.0?
In the old driver it was available via Server.Ping()
? Also, Is there a way to find out if the server is running/responding without running the actual query?
Using mongoClient.Cluster.Description.State
doesn't help because it still gave the disconnected state even after the mongo server started responding.
Asked
Active
Viewed 5,155 times
7

shA.t
- 16,580
- 5
- 54
- 111

Heena Patel
- 81
- 1
- 4
-
possible duplicate of [MongoServer.State equivalent in the 2.0 driver](http://stackoverflow.com/questions/29459990/mongoserver-state-equivalent-in-the-2-0-driver) – i3arnon Jun 08 '15 at 15:47
-
why would you use ping instead of a controlled connection timeout? If you have an high ping, when you will connect to the server your "total time to connect to the db" will be doubled (ping time + real connection time) – Gianni B. Jun 08 '15 at 15:53
-
mongoClient.Cluster.Description.State is not reliable - noticed even after the server was back we were getting the state as disconnected. I was hoping to use the Ping to check the quick connectivity test and throw exceptions and not proceed to the query to differeniate connectivity exceptions vs occasional query/command timeout because of delayed return the response due to number of records. – Heena Patel Jun 08 '15 at 17:53
3 Answers
3
You can check the cluster's status using its Description
property:
var state = _client.Cluster.Description.State
If you want a specific server out of that cluster you can use the Servers
property:
var state = _client.Cluster.Description.Servers.Single().State;

i3arnon
- 113,022
- 33
- 324
- 344
-
I tried that but even after the server came online the state was coming back as Disconnected. – Heena Patel Jun 08 '15 at 17:39
-
@HeenaPatel AFAIK the this reflects the status of the last operation. To actually check the connection you need to make an active operation and see if it fails. – i3arnon Jun 08 '15 at 17:41
-
1mongoClient.Cluster.Description.State is not reliable - noticed even after the server was back we were getting the state as disconnected. I was hoping to use the Ping to check the quick connectivity test and throw exceptions and not proceed to the query to differentiate connectivity exceptions vs occasional query/command timeout because of delayed return the response due to number of records. – Heena Patel Jun 08 '15 at 17:59
-
There is no Cluster property under Client when specifying a connection string. (v2.0.1.27) – Adam Reed Aug 06 '15 at 16:05
-
-
@AdamReed does your `_client` data member happen to be declared as `IMongoClient` instead of `MongoClient`? (good theme though) – i3arnon Aug 06 '15 at 18:44
-
@AdamReed don't really know what to tell you. I see it on my machine and [in the repository on GitHub](https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/MongoClient.cs#L83) (where it is for almost a year): – i3arnon Aug 06 '15 at 21:09
2
This worked for me on both c# driver 2 and 1
int count = 0;
var client = new MongoClient(connection);
// This while loop is to allow us to detect if we are connected to the MongoDB server
// if we are then we miss the execption but after 5 seconds and the connection has not
// been made we throw the execption.
while (client.Cluster.Description.State.ToString() == "Disconnected") {
Thread.Sleep(100);
if (count++ >= 50) {
throw new Exception("Unable to connect to the database. Please make sure that "
+ client.Settings.Server.Host + " is online");
}
}

SBailey
- 21
- 2
0
As @i3arnon's answer I can tell it was reliable for me in this way:
var server = client.Cluster.Description.Servers.FirstOrDefault();
var serverState = ServerState.Disconnected;
if (server != null) serverState = server.State;
or in new versions of .Net
var serverState = client.Cluster.Description.Servers.FirstOrDefault()?.State
?? ServerState.Disconnected;
But if you realy want to run a ping command you can do it like this:
var command = new CommandDocument("ping", 1);
try
{
db.RunCommand<BsonDocument>(command);
}
catch (Exception ex)
{
// ping failed
}

shA.t
- 16,580
- 5
- 54
- 111