1

我已經開始使用MongoDB .Net driver將WPF應用程序連接到MongoLabs上託管的MongoDB數據庫。如何在MongoDB連接期間解決System.TimeoutException?

但是,我創建加載連接(在MainViewModel的構造函數上調用)創建以下方法,在下面的方法中標記的行上拋出超時異常。

我試圖通過添加一個類型爲MongoException的異常檢查來解決錯誤沒有用。還檢查連接字符串是有效的按照該文檔,似乎這樣:(密碼出演出於安全)

private const string connectionString = "mongodb://<brianVarley>:<********>@ds048878.mongolab.com:48878/orders"; 

引發的特定錯誤如下:

An exception of type 'System.TimeoutException' occurred in mscorlib.dll 

完全錯誤鏈接: http://hastebin.com/funanodufa.tex

有沒有人知道我爲什麼我的連接方法超時的原因?

 public List<Customer> LoadCustomers() 
     { 
      var client = new MongoClient(connectionString); 
      var database = client.GetDatabase("orders"); 
      //Get a handle on the customers collection: 
      var collection = database.GetCollection<Customer>("customers"); 

      try 
      { 
       //Timeout error thrown at this line: 
       customers = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult(); 
      } 
      catch(MongoException ex) 
      { 
       //Log exception here: 
       MessageBox.Show("A handled exception just occurred: " + ex.Message, "Connection Exception", MessageBoxButton.OK, MessageBoxImage.Warning);   
      } 

      return customers; 
     } 
+0

您是否嘗試過通過蒙戈外殼或其他方法連接,以確定它是否是一個服務器的問題,而不是你的代碼? – womp

+0

不,實際上好的建議,現在會給我一個去吧 –

+0

我認爲你的蒙哥拉布憑證是無效的。幾周前我有同樣的問題。您是否爲數據庫創建了用戶,或者您是否可以使用您的mongolab帳戶的憑據? –

回答

1

通過重新編輯我的連接字符串解決了這個錯誤。我錯誤地在連接字符串中留下了這兩個符號,'<'和'>'在用戶名和密碼憑證之間。

正確的格式:

"mongodb://brianVarley:[email protected]:54118/orders"; 

格式不正確:

"mongodb://<brianVarley>:<password;>@ds054118.mongolab.com:54118/orders";