【发布时间】:2015-12-04 18:57:17
【问题描述】:
我添加了一个异步任务,它使用MongoDB.Net driver 将列表分配给新的 Bson 文档。我收到有关该方法的警告,说我应该将 await 运算符添加到 API 调用中。
所以我尝试的是,在 API 调用中添加 await,但它给了我一个错误:
错误 9 无法等待 'System.Collections.Generic.List'
我知道我不能等待列表类型,但不确定该运算符的其他位置。我在想 Find 调用可以重构为一个任务,然后将客户分配给它的结果。
客户列表为参考类型。
有谁知道我应该如何将 await 运算符添加到 API 调用中?
这是我在方法中添加 await 运算符的地方:
public async Task LoadDb()
{
var customerCollection = StartConnection();
try
{
customers = await customerCollection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();
}
catch (MongoException ex)
{
//Log exception here:
MessageBox.Show("A connection error occurred: " + ex.Message, "Connection Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
这是 customerCollection 来自的StartConnection():
public IMongoCollection<CustomerModel> StartConnection()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<CustomerModel>("customers");
return collection;
}
【问题讨论】:
标签: c# asynchronous task mongodb-.net-driver