c# - GetSnapshotAsync() 永远不会返回,我也没有收到任何错误。我该如何调试这个问题?
问题描述
我正在尝试使用金块包 Google.Cloud.Firestore 的 Firestore
我跟着这个介绍CRUD 和 firestore
使用调试器我可以看到程序的执行在尝试调用方法 Query.GetSnapshotAsync() 时停止
我只想从集合中获取文档列表。
控制器
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();
}
}
}
餐厅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Google.Cloud.Firestore;
namespace FirestoreAPI.Repository.Restaurants
{
[FirestoreData]
public class Restaurant
{
[FirestoreProperty]
public decimal avgRating { get; set; }
[FirestoreProperty]
public string category { get; set; }
[FirestoreProperty]
public string city { get; set; }
[FirestoreProperty]
public string name { get; set; }
[FirestoreProperty]
public int numRatings { get; set; }
[FirestoreProperty]
public int price { get; set; }
}
}
解决方案
我遇到了同样的问题。问题是,我正在从同步方法中调用异步方法。直到我使调用者方法异步并等待它之后,它才起作用。
推荐阅读
- java - 如何使用 PDFBox 更改嵌入资源的字体
- reactjs - 如何存根 Ant Design 的表单 getFieldDecorator()?
- verilog - always 块中的第一个 if 语句最初总是评估为 true
- javascript - 处理画布视图上的滚动 - paperjs
- c# - 用于发布和验证 JWT 的自定义令牌服务
- python - 从嵌套列表生成字典,Python 3.6
- javascript - 如何检查一个类是否有一些符号,如果有,用它做些什么?
- python - 使用 Pandas 按唯一值拆分并附加为新列
- python-3.x - 选择用于在散景服务器上过滤的小部件
- machine-learning - 卷积神经网络中边缘检测矩阵背后的直觉