繁体   English   中英

Newtonsoft.Json.ReferenceLoopHandling.忽略反序列化格式 .NET Core 3.1

[英]Newtonsoft.Json.ReferenceLoopHandling.Ignore deserialization format .NET Core 3.1

I'm trying to make an API using .NET Core 3.1 with C# programming language which utilizes Firebase real-time database from Google through FireSharp Nu-Get package. 当我运行它时,我得到了这个异常: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.在这里读到我可以只安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson package 并忽略参考循环处理,这工作正常。 但是数据格式看起来不可读,很难反序列化,而且太长了:

{
"stateMachine": {
    "<>1__state": 0,
    "<>t__builder": {},
    "bpjs": "12345678",
    "reservationTime": "24-11-2020",
    "<>4__this": {}
},
"context": {},
"moveNextAction": {
    "method": {
        "name": "MoveNext",
        "declaringType": ...
...
((lots of irrelevant things))
...

    }
},
"result": {
    "queueNumber": "1",
    "bpjs": "12345678",
    "name": "Addi",
    "poli": "Umum",
    "reservationTime": "24-11-2020",
    "status": "Queueing"
},
"id": 2,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false}

在此之前,我已经尝试按照此视频的 DTO 教程进行一些映射,而不是直接使用我的数据 model,但它很可能不起作用,因为我不使用 SQL 数据库。

现在我正在尝试在 Flutter 应用程序上使用这个 API。 无论如何,我是否可以对我的 API 代码(不涉及将数据库完全更改为 SQL 的任何内容)进行一些小的更改以重新格式化我的响应? 或者也许只是部分反序列化我的响应的方法,因为我只需要“结果”部分而不需要其他任何东西?

编辑:

这是我获取数据的主要代码:

        public async Task<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        //set up connection
        IFirebaseConfig config = new FirebaseConfig
        {
            AuthSecret = myAuthSecret,
            BasePath = myBasePath
        };

        IFirebaseClient client = new FireSharp.FirebaseClient(config);

        //checkqueuenumber
        string queueCounter = await QueueDbConnection.CheckQueueNumber(reservationTime, client);

        //getresult
        QueueData result = await QueueDbConnection.GetResult(bpjs, reservationTime, queueCounter, client);
        return result;
    }

这是我的 controller 调用上面的 function:

        [HttpGet("{bpjs}/{reservationTime}")]
    public ActionResult<QueueData> GetQueueData(string bpjs, string reservationTime)
    {
        var queueData = _repository.GetQueueData(bpjs, reservationTime);
        return Ok(queueData);
    }

这是我的 QueueData 的 model,这是我试图作为响应返回的类型:

    public class QueueData
{
    [Required]
    public string QueueNumber { get; set; }
    [Required]
    public string BPJS { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Poli { get; set; }
    [Required]
    public string ReservationTime { get; set; }
    [Required]
    public string Status { get; set; }
}

这是另一个 model class 调用控件来监控每天排队的人数:

    public class QueueCounter
{
    [Required]
    public string q { get; set; }
}

所以,我解决了这个问题。 我回到这里,显然有人说我首先遇到的错误是因为并非所有异步函数都已被等待或正确处理。 所以我再次检查了我的代码,最后在我的 controller 中发现了问题。 在我使我的 controller 异步然后将返回类型更改为 Task<ActionResult> 之后,一切都很好!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM