【问题标题】:Object graph for type 'Entities.Models.ConsignmentDetails' contains cycles and cannot be serialized if references are not tracked'Entities.Models.ConsignmentDetails' 类型的对象图包含循环,如果不跟踪引用,则无法序列化
【发布时间】:2020-05-19 02:55:20
【问题描述】:

检测到类型为“Entities.Models.ConsignmentDetails”的自引用循环。路径 'consignmentDetails[0].Consignee.ConsignmentDetails'.
我可能尝试过使用互联网上的每个解决方案进行锻炼,但无法找到我出错的地方,或者我已经完全搞砸了
在调试时,我得到的是从收货人到寄售详细信息然后再从收货人到寄售详细信息的无限循环数据,并且它继续
我无法更改我的模型类,因为我需要在获取 consignee 时获取 consignmentdetails,并且在获取 consignmentdetails 时我还需要 consignee

这是我的寄售详情模型

namespace Entities.Models
{
    //[ScriptIgnore]
    [DataContract(IsReference = true)]
    public partial class ConsignmentDetails
    {
        public int ConsignmentDetailsId { get; set; }
        public int DocketNo { get; set; }
        public int? ConsignmentValue { get; set; }
        public int? Freight { get; set; }
        public int? Aoc { get; set; }
        public int? ConsigneeId { get; set; }
        .
        . 

        public virtual Consignees Consignee { get; set; }
    }
}

这是我的收货人模型

namespace Entities.Models
{
    [DataContract(IsReference = true)]
    public partial class Consignees
    {
        public int ConsigneeId { get; set; }
        .
        .

        public virtual ICollection<ConsignmentDetails> ConsignmentDetails { get; set; }
    }
}

我正在尝试做的内部控制器

 public IActionResult GetConsigneeWithDetails(int id)
        {
            try
            {
                var consignee = _repository.consignees.GetConsigneesWithDetails(id);
                if (consignee == null)
                {
                    _logger.LogError($"Consignee With Id: {id} hasn't been foud inn the db");
                    return NotFound();
                }
                else
                {
                    _logger.LogInfo($"Returned Consignee with Details for id: {id}");

                    var consigneeResult = _mapper.Map<ConsigneeDTO>(consignee);
                   JsonConvert.SerializeObject(consigneeResult);


                    return Ok(consigneeResult);
                }
            }
            catch(Exception ex)
            {
                _logger.LogError($"SOmething Went Wrong inside GetConsigneeWithDetails action: {ex.Message}");
                return StatusCode(500, "Internal Server Error");
            }
        }

这就是我的控制器获取数据的方式

public Consignees GetConsigneesWithDetails(int id)
        {
            return FindByCondition(consignee => consignee.ConsigneeId.Equals(id)).Include(d => d.ConsignmentDetails).FirstOrDefault();
        }

上面的 consignmentdetails 模型有 20 多个属性,我得到的响应是

{
  "consigneeId": 5,
  "consigneeName": "ABC",
  "consigneeGstno": "AFLPG-8314-H",
  "consignmentDetails": [
    {
      "$id": "1"
    }
  ]
}

【问题讨论】:

    标签: asp.net-core asp.net-core-webapi


    【解决方案1】:

    添加对 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的包引用。

    在您的 Startup.cs 中配置它,如下所示:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
    
        services.AddControllersWithViews()
                .AddNewtonsoftJson(options=>
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            ); 
    
        ...
    }
    

    【讨论】:

    • 我需要在我的模型类中使用[DataContract(IsReference = true)]
    • [DataContract(IsReference = true)] 用于 XmlDCSerializer。请注意:在类上应用 DataContract 后,您需要将 DataMember 添加到要序列化的属性中。更多详情请参考此链接:stackoverflow.com/a/18223985/11965297
    猜你喜欢
    • 1970-01-01
    • 2012-11-08
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2012-12-01
    相关资源
    最近更新 更多