簡體   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