Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

c# - MongoServer.State equivalent in the 2.0 driver

In the old API (1.X) you could tell whether the server was connected or not by using the State property on the MongoServer instance returned from MongoClient.GetServer:

public bool IsConnceted
{
    get
    {
        return _client.GetServer().State == MongoServerState.Connected;
    }
}

However GetServer is not a part of the new API (2.0). How can that be achieved?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The more appropriate way to do that is not by checking the server but rather the cluster (which may contain multiple servers) and you can access it directly from the MongoClient instance:

public bool IsClusterConnceted
{
    get
    {
        return _client.Cluster.Description.State == ClusterState.Connected;
    }
}

If you would like to check a specific server that's also possible:

public bool IsServerConnceted
{
    get
    {
        return _client.Cluster.Description.Servers.Single().State == ServerState.Connected;
    }
}

Keep in mind that the value is updated by the last operation so it may not be current. The only way to actually make sure there's a valid connection is to execute some kind of operation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...