Andy
Andy

Reputation: 349

Checking if a MongoDB server is up without knowing any databases

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!

Upvotes: 4

Views: 3800

Answers (3)

Maxim Popov
Maxim Popov

Reputation: 165

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

Upvotes: 1

Andy
Andy

Reputation: 349

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.

Upvotes: 3

Ravi Kumar Gupta
Ravi Kumar Gupta

Reputation: 1798

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.

Upvotes: 0

Related Questions