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

本文关键字:解决 System TimeoutException 连接 MongoDB | 更新日期: 2023-09-27 18:21:47

我已经开始使用MongoDB.Net驱动程序将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;
        } 

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

通过重新编辑我的连接字符串解决了此错误。我在连接字符串中错误地留下了这两个符号,'<'以及用户名和密码凭据之间的'>'。

正确格式:

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

格式不正确:

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