首页 > 解决方案 > 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; }
    }
}

标签: c#asynchronousasp.net-web-apigoogle-cloud-firestore

解决方案


我遇到了同样的问题。问题是,我正在从同步方法中调用异步方法。直到我使调用者方法异步并等待它之后,它才起作用。


推荐阅读