如何在MongoDB连接期间解决System.TimeoutException?

时间:2015-11-13 18:55:20

标签: c# wpf database-connection mongodb-.net-driver connection-timeout

我已经开始使用MongoDB .Net driver将WPF应用程序连接到MongoLabs上托管的MongoDB数据库。

但是我创建的以下方法来加载连接(在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;
        } 

1 个答案:

答案 0 :(得分:1)

通过重新编辑我的连接字符串解决了这个错误。我错误地将这两个符号留在我的连接字符串中,'&lt;'和'&gt;'用户名和密码凭证之间。

格式正确:

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

格式不正确:

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