4

I'm creating a MongoDB connector for an application, and was wondering if it's possible to check if a MongoDB server is up without knowing any databases.

Every example I have seen so far requires deprecated methods or a database on the MongoDB server to be known.

An example of what I'm trying to do can be seen below;

How to check connection to mongodb

One way I thought about doing this was to use;

ListDatabaseNames()

catching any exceptions that relate to a connection failing. However, this seems like a bit of a 'dirty' solution, as I would also have to catch all exceptions relating to invalid permssions to run the command.

Perhaps, what I'm trying to do, doesn't make sense. If that's the case, please do say!

Andy
  • 339
  • 3
  • 17
  • Can you check this link https://stackoverflow.com/questions/28835833/how-to-check-connection-to-mongodb – Saravanakumar Natarajan Feb 04 '19 at 17:20
  • @SaravanakumarNatarajan, thanks for the reply. That's the link that I added as part of the question. In all of those examples, they either use deprecated methods, or know the database name. GetServer() is the deprecated method. – Andy Feb 04 '19 at 17:22

3 Answers3

3

After looking at the following stack overflow submissions;

MongoServer.State equivalent in the 2.0 driver

C# MongoDB.Driver GetServer is Gone, What Now?

How to check connection to mongodb

I have come to realise that it isn't really possible to ping the server/cluster directly. To get around this, I have done the following;

public bool checkConnection(string connection_string)
{
    var client = new MongoClient(connection_string)

    try 
    {
        client.ListDatabaseNames();
    }
    catch (Exception)
    {
    }

    return client.Cluster.Description.State == ClusterState.Connected;
}

This should deal with any permission issues, as it should return connected even if a user doesn't actually have permission to run;

client.ListDatabaseNames();

If using the above, additional checks should be made to ensure the MongoClient isn't null.

Andy
  • 339
  • 3
  • 17
0

There is a c# implementation to Ping command of mongodb.

As described here - http://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/T_MongoDB_Driver_Core_Operations_PingOperation.htm, PingOperation is available in MongoDB.Driver.Core assembly.

You should be able to use something similar to -

public void Ping()
{
    var messageEncoderSettings = GetMessageEncoderSettings();
    var operation = new PingOperation(messageEncoderSettings);

    var server = GetServer();
    using (var channelSource = new ChannelSourceHandle(new ServerChannelSource(server)))
    using (var channelSourceBinding = new ChannelSourceReadWriteBinding(channelSource, ReadPreference.PrimaryPreferred))
    {
        operation.Execute(channelSourceBinding, CancellationToken.None);
    }
}

Source : C# Example

This also depends on the driver you are using. I would suggest to look into available classes in your mongodb driver.

Ravi Kumar Gupta
  • 1,698
  • 2
  • 22
  • 38
  • Thanks for the reply. Unfortunately, the above uses the same deprecated GetServer() method, something that isn't present. I've looked into this in greater detail, and it seems that you can't directly check if a server/cluster is available. I've added an answer to my own question. – Andy Feb 05 '19 at 09:17
0

I was able to make use of DescriptionChanged event of ICluster interface (returned by MongoClient.Cluster property getter):

private Task WaitForClusterStartup(ICluster cluster)
{
    var tcs = new TaskCompletionSource();

    cluster.DescriptionChanged += (sender, args) =>
    {
        if (args.NewClusterDescription.State == ClusterState.Connected)
        {
            tcs.SetResult();
        }
    };

    return tcs.Task;
}
Maxim Popov
  • 145
  • 7