How to get connection status in the C# MongoDB driver v2.0? -


we starting using new mongodb driver v2 , can't understand whether connected db or not.

our repository code:

var client = new mongoclient("mongodb://{wrong-host}:{wrong-port}/{dbbname}"); var database = client.getdatabase(url.databasename); 

where wrong-host , wrong-port invalid values.

first thought exception raised if no 1 listening on specified address driver doesn't throws.

next step invoke method on db:

var dbs = client.listdatabasesasync().result.tolistasync().result; 

here have freez 30 seconds , exception. not suitable wait 30 seconds know connected or not.

system.timeoutexception: timeout occured after 30000ms selecting server using compositeserverselector{ selectors = readpreferenceserverselector{ readpreference = { mode = primary, tagsets = [] } }, latencylimitingserverselector{ allowedlatencyrange = 00:00:00.0150000 } }. client view of cluster state { clusterid : "1", type : "unknown", state : "disconnected", servers : [{ serverid: "{ clusterid : 1, endpoint : "****" }", endpoint: "****", state: "disconnected", type: "unknown", heartbeatexception: "mongodb.driver.mongoconnectionexception: exception occurred while opening connection server. ---> system.net.sockets.socketexception: no connection made because target machine actively refused ******

finally tried set different timeouts 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   }); 

so question how know whether connected 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 connect to: {0}.", connectionstring));    return db; } 

usage

database = connect(connectionstring, timespan.fromseconds(1)); 

there next workaround issue:

var client = new mongoclient(new mongoclientsettings {        server = new mongoserveraddress("xxxx"),        clusterconfigurator = builder =>        {              builder.configurecluster(settings => settings.with(serverselectiontimeout: timespan.fromseconds(10)));        } }); 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -