C#MongoDB FindAsync从未在等待时返回

C#MongoDB FindAsync从未在等待时返回,c#,mongodb,azure,C#,Mongodb,Azure,还有其他类似的问题,但据我所知,与实际的FindAsync无关 MyClientsController调用ClientService.GetClients,它使用mongo驱动程序查询Azure上的mongodb 逐步通过调试器,直到调用clientCollection.FindAsync。如果我跨过这一步,就永远不会命中下面的行,也不会给出错误。就好像等待的任务永远不会回来 public async Task<List<Client>> GetClients(Searc

还有其他类似的问题,但据我所知,与实际的FindAsync无关

My
ClientsController
调用
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;
}

因为并没有太多关于上下文的信息,所以我提出了模拟类来解决这个问题

请看下面的重载方法,调用时,它将始终返回三条记录的列表。现在你的代码出了什么问题?我相信这在你的while循环中。您正在调用引起问题的
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;
}
public异步任务GetClients()
{
var mongoUri=”mongodb://localhost:27017";
var mongoClient=新mongoClient(mongoUri);
var mongoDatabase=mongoClient.GetDatabase(“ClientDB”);
var clientCollection=mongoDatabase.GetCollection(“客户端”);
//清空集合以始终获得准确的结果。
DeleteMany(新的BsonDocument());
//插入一些虚拟数据
InsertOne(新客户端(){Address=“一条街,一些州”,ZipCode=11111});
InsertOne(新客户端(){Address=“第二街,某些州”,ZipCode=22222});
InsertOne(新客户端(){Address=“第三条街,某个州”,ZipCode=33333});
var response=newlist();
var cursor=await clientCollection.FindAsync(new BsonDocument());
while(等待cursor.MoveNextAsync())
{
response.AddRange(cursor.Current);
}
返回响应;
}

在下一行设置断点。当涉及到线程切换时,调试器很可能会丢失对任务继续的跟踪。听起来就是这样。但是当我
继续
它是否与synchron调用一起工作时,它从未到达下一行?如果调用Find()并使用MoveNext在游标上迭代,您会得到任何文档吗?或者您的集合是空的?同步调用可以工作。集合中有1项。您使用的驱动程序版本是什么?
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;
}