c# - 如何在 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”}]}。”
我做错了什么?
解决方案
我尝试使其异步并且可以正常工作。
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();
}
有没有办法让它同步工作?
推荐阅读
- python - 是否可以以打字稿样式运行异步 python 脚本
- vuetify.js - Vuetify v-data-table v-if 问题
- c++ - C++ 放置删除如何在内部工作(C++ 运行时)?如何克服它的局限?
- php - WooCommerce 中为 foreach() 提供的参数无效
- winapi - 检测窗口是否正在使用硬件加速的图形上下文
- html - 如何在 flexbox 中居中内容
- c - C 中的尾递归总是比经典递归更快,如果是,我该如何修复我的代码?
- r - 将维恩图转换为 grob?
- javascript - 如何更新 Mongo 文档数组中的对象(在特定索引处)
- directory - 什么词或短语专门表示“..”,即目录导航操作的“转到父目录”参数?