首页 > 解决方案 > 如何在 wpf .NETFramework 中连接 mongodb

问题描述

我有这段代码,它适用于控制台:

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            MongoClient client = new MongoClient();
            var db = client.GetDatabase("test1");
            IMongoCollection<Person> collection = db.GetCollection<Person>("people");
            List<Person> people = collection.Find(new BsonDocument()).ToList();

            foreach (Person person in people)
            {
                //MessageBox.Show($"{person.FirstName} => {person.LastName}");
                Console.WriteLine($"{person.FirstName} => {person.LastName}");
            }
            Console.ReadKey();
        }
    }

    [BsonIgnoreExtraElements]
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

现在是一个简单的 wpf 应用程序的相同代码。

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;

namespace MongoWpf
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MongoClient client = new MongoClient();
            var db = client.GetDatabase("test1");
            IMongoCollection<Person> collection = db.GetCollection<Person>("people");
            List<Person> people = collection.Find(new BsonDocument()).ToList();

            foreach (Person person in people)
            {
                MessageBox.Show($"{person.FirstName} => {person.LastName}");
                //Console.WriteLine($"{person.FirstName} => {person.LastName}");
            }
        }
    }
    [BsonIgnoreExtraElements]
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

给我 System.TimeoutException

“使用 CompositeServerSelector { Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector { AllowedLatencyRange = 00:00:00.0150000 } } 选择服务器 30000 毫秒后发生超时。集群状态的客户端视图是 { ClusterId : "1", ConnectionMode : "自动”,类型:“未知”,状态:“断开连接”,服务器:[{ ServerId:“{ ClusterId:1,端点:“未指定/本地主机:27017”}”,端点:“未指定/本地主机:27017”,ReasonChanged :“ServerInitialDescription”,状态:“断开连接”,ServerVersion:,TopologyVersion:,类型:“未知”,LastHeartbeatTimestamp:null,LastUpdateTimestamp:“2020-08-23T15:42:56.2882193Z”}]}。”

我做错了什么?

标签: c#wpfmongodbmongodb-queryconsole

解决方案


我尝试使其异步并且可以正常工作。

        private void button1_Click(object sender, EventArgs e)
        {
            async void a()
            {
                MongoClient client = new MongoClient();
                var db = client.GetDatabase("test1");
                IMongoCollection<Person> collection = db.GetCollection<Person>("people");
                var people = await collection.Find(new BsonDocument()).ToListAsync();
                foreach (Person person in people)
                {
                    MessageBox.Show($"{person.FirstName} => {person.LastName}");
                }
            }
            a();
        }

有没有办法让它同步工作?


推荐阅读