还有其他类似的问题,但与我所知道的实际FindAsync无关。
我的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;
}
调试器永远不会越过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();
}
答案 0 :(得分:0)
由于没有太多关于上下文的信息,所以我提出了模拟类来满足问题。
请参阅下面的重载方法,在调用时,它将始终返回三个记录的列表。现在您的代码出了什么问题?我相信它在你的while循环中。您正在调用导致问题的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;
}
而且有效。
#!/bin/bash
read -p "Number of Adult Tickets:(0 for none) " integer1
read -p "Number of Junior Tickets:(0 for none) " integer2
echo ""
echo "$integer1 Adults"
echo "$integer2 Juniors"
echo ""
num1=$50
num2=$20
num3=$5
echo "Season Pass Add-On:"
echo "1)Add Seasonal Parking"
echo "2)Add Season Pass For Theme Park"
echo "7)Just Buy Tickets For Today"
echo "8)I wanna exit."
echo ""
read -p "What would the guest like to do? " guest
echo ""
case $guest in
1)
echo "Add Seasonal Parking"
echo ""
echo "Add an additional $num1 for every adult pass"
echo ""
echo "For Adult Tickets "$(( integer1 + num1 ))""
echo "For Junior Tickets "$(( integer2 + num1 ))""
;;
2)
echo "Add Season Pass For Theme Park"
echo ""
echo "Add an additional $num2 for every adult pass"
echo "Add an additional $num3 for every junior pass"
echo ""
echo "For Junior Tickets "$(( integer2 * num2 ))""
echo "For Adult Tickets "$(( integer1 * num3 ))""
echo "For Junior Tickets "$(( integer2 * num2 ))""
echo "For Adult Tickets "$(( integer1 * num3))""
;;
3)
echo "Just buy tickets for today"
exit 0
;;
4)
echo "invalid entry. You are exiting"
exit 1
;;
esac
exit 0