我正在尝试使用带有Nugget软件包Google.Cloud.Firestore的Firestore
我关注了此介绍CRUD with firestore
使用调试器,我可以看到在尝试调用方法Query.GetSnapshotAsync()
时,程序的执行停止了。我只想从集合中获取文档列表。
控制器
+--------------+--------+--------+------------+-------+
| id_dependent | name | number | primary_id | Value |
+--------------+--------+--------+------------+-------+
| 51 | Carlos | 956585 | 2 | 15,00 |
| 52 | João | 985868 | 2 | 13,50 |
| 53 | Jaime | 985868 | 2 | 11,50 |
| 54 | Evan | 985847 | 3 | 15,00 |
| 55 | Kaus | 584788 | 3 | 13,50 |
+--------------+--------+--------+------------+-------+
DataLayer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using System.Web.Http.Results;
using FirestoreAPI.Domain.Restaurants;
using FirestoreAPI.Repository.Restaurants;
using System.Threading.Tasks;
namespace FirestoreAPI.Controllers
{
[RoutePrefix("api")]
public class RestaurantsController : ApiController
{
public RestaurantsController() { }
[HttpGet]
[Route("restaurants")]
public IHttpActionResult GetAllRestaurants()
{
//var restAggregate = new RestaurantsAggregate();
var restaurantsRepo = new RestaurantsRepo();
return new NegotiatedContentResult<Task<List<Restaurant>>>(
HttpStatusCode.OK,
restaurantsRepo.GetAllRestaurants(),
this
); ;
}
}
}
餐厅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Google.Cloud.Firestore;
namespace FirestoreAPI.Repository.Restaurants
{
public class RestaurantsRepo
{
//public FirestoreConn dbConn;
string projectId;
FirestoreDb firestoreDb;
public RestaurantsRepo()
{
//dbConn = new FirestoreConn();
string credentials = "C:\\Users\\ezequiel.lopez\\projects\\firestoredotnet\\FirestoreAPI\\firestoreapi-dca55-0be2f7d57f41.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentials);
projectId = "firestoreapi-dca55";
firestoreDb = FirestoreDb.Create(projectId);
}
public async Task<List<Restaurant>> GetAllRestaurants()
{
//FirestoreDb fsDB = dbConn.GetFSConnection();
//Query query = fsDB.Collection("restaurants").OrderByDescending("avgRating").Limit(50);
Query query = firestoreDb.Collection("restaurants").OrderByDescending("avgRating").Limit(50);
QuerySnapshot restSnaps = await query.GetSnapshotAsync();
List<Restaurant> restaurants = new List<Restaurant>();
return restSnaps.Documents
.Where<DocumentSnapshot>(ds => ds.Exists)
.Select(ds => ds.ConvertTo<Restaurant>()).ToList();
}
}
}
答案 0 :(得分:0)
我遇到了同样的问题。问题是,我正在从同步方法中调用异步方法。直到我使调用者方法异步并等待它,它才起作用。