MongoDB C#CreateOneAsync不返回

MongoDB C#CreateOneAsync不返回,c#,mongodb,C#,Mongodb,LogEntry继承LogRecord,LogRecord定义: [BsonRepresentation(BsonType.ObjectId)] internal string parent { get; set; } 数据库和集合中已存在数据,我运行: internal static async void CreateIndex() { try { var collection = db.GetCollection<LogEntry>(ty

LogEntry继承LogRecord,LogRecord定义:

[BsonRepresentation(BsonType.ObjectId)]
internal string         parent { get; set; }
数据库和集合中已存在数据,我运行:

internal static async void CreateIndex() {
    try {
        var collection = db.GetCollection<LogEntry>(typeof(LogEntry).Name);
        var name = await collection.Indexes.CreateOneAsync(
            Builders<LogEntry>.IndexKeys.Ascending(entry => entry.parent));
        Console.WriteLine("index name is {0}", name);
    } catch (Exception exc) {
        Console.WriteLine("oh no");
    }
}
内部静态异步void CreateIndex(){
试一试{
var collection=db.GetCollection(typeof(LogEntry).Name);
var name=await collection.index.CreateOneAsync(
升序(entry=>entry.parent));
WriteLine(“索引名为{0}”,名称);
}捕获(异常exc){
控制台。WriteLine(“哦,不”);
}
}
当调用CreateOneAsync并且Compass在该集合的数据库中没有显示索引时,由于两个writeLine上都有断点,程序只会终止

那么如何确定CreateOneAsync失败的原因呢


解决方案(感谢@Veeram):需要使用。调用CreateOneAsync时产生结果,并在我的CreateIndex方法中删除async/Wait。

将您的方法更新为以下基本同步版本

注意使用
Result
在调用线程中完成调用

internal static void CreateIndex() {
    try {
        var collection = db.GetCollection<LogEntry>(typeof(LogEntry).Name);
        var name =  collection.Indexes.CreateOneAsync(
            Builders<LogEntry>.IndexKeys.Ascending(entry => entry.parent)).Result();
        Console.WriteLine("index name is {0}", name);
    } catch (Exception exc) {
        Console.WriteLine("oh no");
    }
}
内部静态void CreateIndex(){
试一试{
var collection=db.GetCollection(typeof(LogEntry).Name);
var name=collection.index.CreateOneAsync(
Builders.IndexKeys.Ascending(entry=>entry.parent)).Result();
WriteLine(“索引名为{0}”,名称);
}捕获(异常exc){
控制台。WriteLine(“哦,不”);
}
}

@Veeram的可能副本实际上我不明白。我实际上不希望我的CreateIndex是异步的。我必须在它上面加上async关键字,才能将wait与CreateOneAsync一起使用。我在你引用的文章中看到,Wait()的最终用法。(我通过死记硬背尝试了这一点,但没有编译(无法解析符号等待)。)但无论如何,我希望尽快结束异步链。我错过了什么?是的,我想就是这个,谢谢。它已经编译,没有返回,但也没有终止。如果我得到一个索引,我会给你的答案打分。