还有其他类似的问题,但从我所知与实际的findasync无关。
myClientsController调用ClientService.GetClients,后者使用mongo驱动程序在azure上查询mongodb。
通过调试器,它达到了我调用clientCollection.FindAsync的程度。如果我跨过这一步,就不会碰到下面的行,也不会出现错误。就像等待的任务永远不会回来。

public async Task<List<Client>> GetClients(SearchRequestDTO searchRequest)
{
  var response = new List<Client>();

  var db = _databaseUtilityService.GetCoreDatabase();
  var clientCollection = db.GetCollection<Client>(Properties.Settings.Default.ClientCollectionName);

  var cursor = await clientCollection.FindAsync(new BsonDocument());

  while (await cursor.MoveNextAsync())
  {
    response.Concat(cursor.Current.ToList());
  }

  return response;
}

调试器从不跨过var cursor = ...行的原因是什么?
编辑-
我可以换成Result。-
var cursor = clientCollection.FindAsync(new BsonDocument()).Result;
但我不确定我想做什么。
public async Task<List<Client>> GetClients(SearchRequestDTO searchRequest)
{
  var db = _databaseUtilityService.GetCoreDatabase();
  var clientCollection = db.GetCollection<Client>(Properties.Settings.Default.ClientCollectionName);
  var results = clientCollection.FindAsync(new BsonDocument()).Result;

  return results.ToList();
}

最佳答案

由于没有太多关于上下文的信息,所以我提出了模拟类来满足这个问题。
请参阅下面的重载方法,当调用它时,它将始终返回三条记录的列表。你的密码怎么了?我相信这是你的时间循环。您正在呼叫导致问题的response.Concat
我改打电话给response.AddRange,它起作用了。

public async Task<List<Client>> GetClients()
{
    var mongoUri = "mongodb://localhost:27017";
    var mongoClient = new MongoClient(mongoUri);

    var mongoDatabase = mongoClient.GetDatabase("ClientDB");
    var clientCollection = mongoDatabase.GetCollection<Client>("Clients");

    // Empty collection to always get accurate result.
    clientCollection.DeleteMany(new BsonDocument());

    // Insert some dummy data
    clientCollection.InsertOne(new Client() {Address = "One street, some state", ZipCode = 11111});
    clientCollection.InsertOne(new Client() { Address = "2nd street, some state", ZipCode = 22222 });
    clientCollection.InsertOne(new Client() { Address = "Third street, some state", ZipCode = 33333 });

    var response = new List<Client>();
    var cursor = await clientCollection.FindAsync(new BsonDocument());

    while (await cursor.MoveNextAsync())
    {
        response.AddRange(cursor.Current);
    }

    return response;
}

08-17 10:17