Initialize MongoClient with MongoClientSettings in C#
Asked Answered
S

2

13

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.

Studding answered 12/5, 2015 at 9:56 Comment(4)
Do you wait for the async execution using await, Wait() or .Result?Numerous
Yes I do, I edited the code to reflect thatStudding
Have you tried with the default settings?Numerous
Yes, I tried with client = new MongoClient("mongodb://localhost") and it worksStudding
N
15

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");
Numerous answered 12/5, 2015 at 19:56 Comment(0)
L
0

I m doing it like this:

MongoClientSettings settings = MongoClientSettings.FromUrl("mongodb://localhost"));
settings.WaitQueueSize = int.MaxValue;
settings.WaitQueueuTimeout = new TimeSpan(0,2,0);
settings.MinConnectionPoolSize = 1;
settings.MaxConnectionPoolSize = 25;
client = new MongoClient(settings)
Lowboy answered 1/11, 2023 at 9:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.