當我嘗試在FindAll(filter)
方法中使用await
關鍵字時,我最終得到了不可編譯的代碼。例如: -Mongo`await FindAsync`不能編譯。
using (var cursor = await collection.FindAsync(filter))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
// process document
count++;
}
}
}
所賜:
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
如果我看源,該方法確實返回Task
:
public static Task<IAsyncCursor<TDocument>> FindAsync<TDocument>(...)
任何想法是怎麼回事?
就是這樣,謝謝你。這是一種測試方法,它不會發生在我身上! – BanksySan
'async void'?絕對是一個不。使它成爲「異步任務」。 –