ASP.NET的Web API序列化JSON错误:"自参考环路QUOT; [英] ASP.NET Web API Serialized JSON Error: "Self Referencing loop"

查看:206
本文介绍了ASP.NET的Web API序列化JSON错误:"自参考环路QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是一个长期的斗争与这一个。我有一个ASP.NET的Web API合作,提供与数据库清洁容易HTTP / JSON交互。我有一个实体名称保留,看起来如下:

  //预订
公共类预订
{
    公众诠释ID {搞定;组; } // ID(主键)
    公众诠释EquipmentID {搞定;组; } // EquipmentID
    公共字符串用户名{获得;组; } // 用户名
    公众的DateTime BeginDateTime {搞定;组; } // BeginDateTime
    公众诠释?持续时间{搞定;组; } //持续时间
    公众诠释? ReservationStateID {搞定;组; } // ReservationStateID
    公众的DateTime? CheckInDateTime {搞定;组; } // CheckInDateTime
    公共字符串注意{搞定;组; } // 注意    //外键
    公共虚拟设备设备{搞定;组; } // FK_Reservation_EquipmentID
    公共虚拟ReservationState ReservationState {搞定;组; } // FK_Reservation_ReservationState
}

到目前为止好。我运行一个简单的Python脚本来创建一个新的保留,与HTTP请求的预订JSON对象一起传递。在previous生活中,我跑了预约加code无数据验证,并在返回HttpActionResult预约的对象。我的回报看到的是一个很好的JSON对象:


  

{u'Username':u'name',u'ReservationStateID':1,u'Equipment':无,u'EquipmentID':2,u'BeginDateTime':u'2014-05-31T14:00: 00Z',u'Note':U',u'CheckInDateTime':无,u'Duration:10800,u'ReservationState':无,u'ID':51}


我是有点担心的外键设备和ReservationState被包括在返回的对象,这可能会借钱给更大的问题,但在适当的时候。
现在我尝试通过收集我的预订引用的设备项目运行数据验证即可。

 设备equipmentItem =等待db.Equipment.FindAsync(newRes.EquipmentID);

现在,当我尝试执行相同的操作,使用在同一个python脚本相同的数据接收的结果是一个大的吓人错误:


  

在'ObjectContent`1类型没有序列化响应正文内容类型应用/ JSON的;字符集= utf-8'。u'StackTrace':无,u'Message':u'An错误已经发生',u'InnerException:{u'ExceptionMessage:财产U检测自参考循环'设备'类型...


我99%肯定的是,外键到位,我的数据库没有创建循环引用,但很可惜我在这里。 我想再次指出在成功的JSON结果列入外键。我敢打赌,如果我可以摆脱那些我可以摆脱自我引用问题的。感谢您的帮助!


解决方案

该错误是由EF外键的对象创建代理造成的,默认的序列化与代理时,序列化(DataContractSerializer的)错误。解决的办法是以下行添加到文件的Global.asax.cs:

<$p$p><$c$c>GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

虽然@ SKall的回答没有解决的问题,它并没有解决整个项目,只是我给IgnoreDataMember属性的属性。

Been a long struggle with this one. I'm working with an ASP.NET web API to provide clean and easy HTTP/JSON interaction with a database. I have an entity name Reservation with that looks as following:

    // Reservation
public class Reservation
{
    public int ID { get; set; } // ID (Primary key)
    public int EquipmentID { get; set; } // EquipmentID
    public string Username { get; set; } // Username
    public DateTime BeginDateTime { get; set; } // BeginDateTime
    public int? Duration { get; set; } // Duration
    public int? ReservationStateID { get; set; } // ReservationStateID
    public DateTime? CheckInDateTime { get; set; } // CheckInDateTime
    public string Note { get; set; } // Note

    // Foreign keys
    public virtual Equipment Equipment { get; set; } // FK_Reservation_EquipmentID
    public virtual ReservationState ReservationState { get; set; } //FK_Reservation_ReservationState
}

So far so good. I am running a simple python script to create a new reservation, passing along with the http request a Reservation JSON object. In a previous life I ran the reservation adding code without data validation and returned the Reservation object in the HttpActionResult. What I saw in return was a nice json object:

{u'Username': u'name', u'ReservationStateID': 1, u'Equipment': None, u'EquipmentID': 2, u'BeginDateTime': u'2014-05-31T14:00:00Z', u'Note': u'', u'CheckInDateTime': None, u'Duration': 10800, u'ReservationState': None, u'ID': 51}

I am a little concerned with the foreign keys Equipment and ReservationState being included in the returned object, which may lend to the larger problem, but that in due time. Now I try to run data validation by gathering the Equipment item referenced by my Reservation.

Equipment equipmentItem = await db.Equipment.FindAsync(newRes.EquipmentID);

Now when I try to perform the same action, using the same data on the same python script the result received is a big scary error:

"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.", u'StackTrace': None, u'Message': u'An error has occurred.', u'InnerException': {u'ExceptionMessage': u"Self referencing loop detected for property 'Equipment' with type...

I am 99% positive that the foreign keys in place in my database do not create circular references, but alas here I am. Again I want to point out the inclusion of the foreign keys in the "successful" json results. I'm betting if I can be rid of those I can be rid of the self referencing issue. Thanks for the help!

解决方案

The error was caused by the EF creating proxies for the foreign key objects, the default serializer (DataContractSerializer) errors when serializing with proxies. The solution is to add the following line to the Global.asax.cs file:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 

While @SKall's answer did solve the problem, it didn't solve it throughout the project, just to properties that I gave the IgnoreDataMember attribute.

这篇关于ASP.NET的Web API序列化JSON错误:&QUOT;自参考环路QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆