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";