Initialize MongoClient with MongoClientSettings in C#

Fedor Finkenflugel picture Fedor Finkenflugel · May 12, 2015 · Viewed 10.4k times · Source

I'm trying to initialize the MongoClient from the Mongo 2.0 driver as follows:

MongoClientSettings settings = new MongoClientSettings();
settings.WaitQueueSize = int.MaxValue;
settings.WaitQueueuTimeout = new TimeSpan(0,2,0);
settings.MinConnectionPoolSize = 1;
settings.MaxConnectionPoolSize = 25;
settings.Server = new MongoServerAddress("mongodb://localhost");
client = new MongoClient(settings)

However, when I now try to insert a document with this code:

db = client.GetDatabase("local");
col = db.GetCollection<BsonDocument>(collectionName);
col.InsertOneAsync(new BsonDocument().Add(new BsonElement("id",BsonValue.Create(1)))).Wait();

It doesn't do anything. It doesn't get inserted, and no error message (although after a while a first chance exception of System.Timeout appears in the output). If I initialize the client with

client = new MongoClient("mongodb://localhost")

It does work and uploads the document as intended.

I want the client to be able to handle a very high write throughput, so I tried these settings first. Did I set some of the settings wrong or is there a different problem?

EDIT: After some more testing, it is indeed a System.Timeout exception I'm getting.

Answer

mnemosyn picture mnemosyn · May 12, 2015

I could reproduce the problem, only in my error message, there is some much more helpful information buried somewhere in about 40 lines of text:

No such host is known

It turns out that MongoServerAddress only expects the hostname, not the protocol:

settings.Server = new MongoServerAddress("localhost");