mongo client settings throwing exception

Viewed 33

I am using .Net to connect to a mongo db, when I use client settings as per the code block below I get an exception (below the code block)

var clientSettings = new MongoClientSettings()
{
    ConnectTimeout = TimeSpan.FromSeconds(30),
    SocketTimeout = TimeSpan.FromSeconds(30),
    WaitQueueTimeout = TimeSpan.FromSeconds(30),
    Server = new MongoServerAddress("host", portnumber)
};
var dbClient = new MongoClient(clientSettings);
var _mongoDb = dbClient.GetDatabase(db);
var mongoCollection = _mongoDb.GetCollection<T>(collection);


// exists with code 0 when not able to return with anything
var retVal =  await mongoCollection.Find(filter).ToListAsync();
return retVal;

Exception :

One or more errors occurred. (A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Automatic", Type : "Unknown", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/xxxx-mongoapi.documents.azure.com:10255" }", EndPoint: "Unspecified/xxxx-mongoapi.documents.azure.com:10255", State: "Disconnected", Type: "Unknown", LastUpdateTimestamp: "2022-09-16T11:18:00.8150834Z" }] }.)

However, if I just add the connection string like this

var dbClient = new MongoClient(connectionString);

Then this connects no problem.

Can anyone suggest why \ where I have gone wrong please.

Thanks

Simon

1 Answers

As it mentioned in the comments, you specify different options in connection string and MongoClientSettings, to check it, compare MongoClientSettings what you fill manually and options you will have with MongoClientSettings.FromConnectionString():

var settings = new MongoClientSettings() { .. };

and

var settings = MongoClientSettings.FromConnectionString(connectionString)
Related