Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize MongoClient with MongoClientSettings in C#

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.

like image 804
Fedor Finkenflugel Avatar asked May 12 '15 09:05

Fedor Finkenflugel


1 Answers

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");
like image 65
mnemosyn Avatar answered Oct 06 '22 14:10

mnemosyn