【问题标题】:GetSnapshotAsync() never returning and I don't get any errors. How can I debug this issue?GetSnapshotAsync() 永远不会返回,我也没有收到任何错误。我该如何调试这个问题?
【发布时间】:2019-07-29 20:44:18
【问题描述】:

我正在使用块包 Google.Cloud.Firestore 尝试 Firestore

我关注了这个介绍CRUD with 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# asynchronous asp.net-web-api google-cloud-firestore


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-07
      • 2021-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 1970-01-01
      相关资源
      最近更新 更多