2015-05-12 62 views
4

我試圖從蒙戈2.0驅動程序初始化MongoClient如下:初始化MongoClient在C#MongoClientSettings

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) 

然而,當我現在嘗試插入文檔使用此代碼:

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

它什麼也沒做。它沒有被插入,也沒有錯誤信息(儘管一段時間後輸出中出現了System.Timeout的第一次機會異常)。如果我初始化客戶端

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

它確實工作並按預期上載文檔。

我希望客戶端能夠處理非常高的寫入吞吐量,所以我先嚐試了這些設置。我是否設置了一些錯誤的設置或存在其他問題?

編輯:經過一些更多的測試,它確實是一個System.Timeout異常我得到。

+0

你等待使用異步執行'await','等待()'或'.Result'? – mnemosyn

+0

是的,我編輯代碼以反映 –

+0

您是否嘗試過使用默認設置? – mnemosyn

回答

8

我可以重現該問題,只存在於我的錯誤信息,有大約40行文本的地方埋藏一些更爲有用的信息:

沒有這樣的主機被稱爲

它原來,MongoServerAddressonly expects the hostname, not the protocol

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