如何在C#MongoDB驱动程序v2.0中获取连接状态? [英] How to get connection status in the C# MongoDB driver v2.0?
问题描述
我们开始使用新的 MongoDB驱动程序v2 ,但我们无法了解我们是否已连接到数据库.
We are starting using new MongoDB driver v2 and we can't understand whether we are connected to the db or not.
我们的存储库代码:
var client = new MongoClient("mongodb://{wrong-host}:{wrong-port}/{dbbname}");
var database = client.GetDatabase(url.DatabaseName);
wrong-host
和wrong-port
是无效值.
首先我们认为,如果没有人正在侦听指定地址但驱动程序未引发,则会引发异常.
First we thought that exception will be raised if no one is listening on specified address but driver doesn't throws.
下一步是在db上调用方法:
Next step was to invoke method on the db:
var dbs = client.ListDatabasesAsync().Result.ToListAsync().Result;
在这里我们有30秒钟的冻结时间,而且比异常情况要多.我们不适合等待30秒来了解我们是否连接.
Here we have freez for 30 seconds and than exception. It was not suitable for us to wait 30 seconds to get to know connected we are or not.
System.TimeoutException:选择30000ms后发生超时 使用CompositeServerSelector {选择器= ReadPreferenceServerSelector {ReadPreference = {模式=主要, TagSets = []}},LatencyLimitingServerSelector {AllowedLatencyRange = 00:00:00.0150000}}.群集状态的客户端视图为{ClusterId: "1",类型:未知",状态:已断开连接",服务器:[{ServerId: "{ClusterId:1,EndPoint:" ****}",EndPoint:"****",状态: 已断开连接",类型:未知",HeartbeatException: "MongoDB.Driver.MongoConnectionException:发生异常时 打开与服务器的连接. ---> System.Net.Sockets.SocketException:无法建立连接 因为目标计算机主动拒绝了它******
System.TimeoutException: A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode = Primary, TagSets = [] } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "****" }", EndPoint: "****", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it ******
最后,我们尝试设置不同的超时时间,但未发生任何变化.
Finally we tried to set up different timeouts but nothing changed.
var client = new MongoClient(new MongoClientSettings
{
SocketTimeout = TimeSpan.FromSeconds(1),
MaxConnectionIdleTime = TimeSpan.FromSeconds(1),
MaxConnectionLifeTime = TimeSpan.FromSeconds(1),
ConnectTimeout = TimeSpan.FromSeconds(1),
Servers = url.Servers
});
所以问题是我们如何才能知道在短的〜(1-2)秒内是否已连接到mongo?
So the question is how we could know whether we are connected to the mongo or not in short time interval ~(1-2) seconds?
[UPD]
我们当前的解决方案是:
Our current solution is:
private IMongoDatabase Connect(string connectionString, TimeSpan timeout)
{
var url = MongoUrl.Create(connectionString);
var client = new MongoClient(url);
var db = client.GetDatabase(url.DatabaseName);
var pingTask = db.RunCommandAsync<BsonDocument>(new BsonDocument("ping", 1));
pingTask.Wait(timeout);
if (pingTask.IsCompleted)
log.InfoFormat("Connected to: {0}.", connectionString);
else
throw new TimeoutException(string.Format("Failed to connect to: {0}.", connectionString));
return db;
}
用法
database = Connect(connectionString, TimeSpan.FromSeconds(1));
推荐答案
此问题:
var client = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("xxxx"),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(10)));
}
});
这篇关于如何在C#MongoDB驱动程序v2.0中获取连接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!