【问题标题】:.NET C# retrieving data from Firebase.NET C# 从 Firebase 检索数据
【发布时间】:2020-09-08 21:37:57
【问题描述】:

我在从 Firebase 上托管的数据库中将数据检索到我的 .net 应用程序时遇到了一些问题。在调试时,我注意到response 包含[{"Description":"desccc","Id":2},{"Description":"asdfasdf","Id":1}],这是我数据库中的唯一记录,所以我认为目前一切正常。不幸的是,当尝试将此数据放入 result 时,它会引发如下错误:

{System.MissingMethodException:没有为此对象定义无参数构造函数。在 System.Activator.CreateInstanceT at RestSharp.Deserializers.JsonDeserializer.Deserialize[T](IRestResponse response) at Rate_Your_Game.Controllers.GameController.GetGames() 在 PATH/Rate Your Game\Controllers\GameController.cs:line 49}

[HttpGet("[action]")]
public async Task<Game[]> GetGames()
{
    try
    {
        client = new FireSharp.FirebaseClient(config);
        FirebaseResponse response = await client.GetTaskAsync("Games/");
        Game[] result = response.ResultAs<Game[]>();
        return result;
    }
    catch (Exception e)
    {
        throw e;
    }
}

这是我的游戏课

public class Game
    {
        public Game() {}

        public int Id { get; set; }
        public string Description{ get; set; }    
        
    }

【问题讨论】:

  • 为什么 null 会出现在结果中?
  • Game 类需要有一个没有参数的默认构造函数才能反序列化。
  • @VimalCK null 即将到来,因为我的 firebase 中的第一条记录具有键“1”,它可能认为它可以从 0 开始。当我手动添加键为 0 的记录时,null 消失了跨度>

标签: c# .net firebase firebase-realtime-database


【解决方案1】:

Game 类需要有无参构造函数。

public class Game {
     public Game(){
     }

     ....
}

【讨论】:

  • 我的类看起来像这样 - 空的无参数构造函数和 uderneath 我有字段:Id 和 Description。错误仍然出现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多