循环引用检测异常,而对象序列化到JSON [英] Circular reference detected exception while serializing object to JSON

查看:335
本文介绍了循环引用检测异常,而对象序列化到JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如在<一个提到

href=\"http://stackoverflow.com/questions/5588143/ef-4-1-$c$c-first-json-circular-reference-serialization-error\">this后,我得到一个JSON序列化的错误而序列化实体框架代理:


  

而序列化类型的对象时检测到循环引用
  'System.Data.Entity.DynamicProxies.PurchaseOrder_446B939192F161CDBC740067F174F7A6059B0F9C0EEE68CD3EBBD63CF9AF5BD0'.


但不同的是,我的在我的实体循环引用,它的发生在我们的生产环境。当地一切正常......

我的实体:

 公共接口IEntity
{
    GUID唯一ID {搞定; }
    INT标识{搞定; }
}公共类实体:IEntity
{
    公众诠释标识{搞定;组; }
    公众的Guid唯一ID {搞定;组; }
}公共类的PurchaseOrder:实体
{
    公共字符串用户名{获得;组; }
    公共字符串公司{搞定;组; }    公共字符串供应商ID {搞定;组; }
    公共字符串SupplierName {搞定;组; }    公共虚拟的ICollection&LT; Pur​​chaseOrderLine&GT;行{搞定;组; }
}公共类PurchaseOrderLine:实体
{
    公共字符串code {搞定;组; }
    公共字符串名称{;组; }
    公共小数数量{搞定;组; }
}

在我PurchaseOrderController的GetCurrent动作抛出异常:

 公共类PurchaseOrderController:控制器
{
    私人只读IUnitOfWork _unitOfWork;    公共PurchaseOrderController(IUnitOfWork的UnitOfWork)
    {
        _unitOfWork =的UnitOfWork;
    }    公共JsonResult GetCurrent()
    {
        返回JSON(EnsurePurchaseOrder(),JsonRequestBehavior.AllowGet);
    }    私人PurchaseOrder的EnsurePurchaseOrder()
    {
        VAR公司= RouteData.GetRequiredString(「本公司」);
        变种库= _unitOfWork.GetRepository&LT;&PurchaseOrder的GT;();        VAR purchaseOrder的=库
                .INCLUDE(P =&GT; p.Lines)
                .FirstOrDefault
                (
                    P =&GT; p.Company ==公司和放大器;&安培;
                         p.Username == User.Identity.Name
                );        如果(purchaseOrder的== NULL)
        {
            purchaseOrder的= repository.Create();
            purchaseOrder.UniqueId = Guid.NewGuid();
            purchaseOrder.Company =公司;
            purchaseOrder.Username = User.Identity.Name;
            _unitOfWork.SaveChanges();
        }        返回purchaseOrder的;
    }
}


解决方案

您POCO实体是完全可序列化。你的问题是,动态代理的EF运行时会为你通常不会。您可以设置context.Configuration.ProxyCreationEnabled为false,但你失去延迟加载。我强烈推荐你是使用Json.NET支持序列化EF实体:

<一个href=\"http://james.newtonking.com/archive/2009/07/10/ado-net-entity-framework-support-accidently-added-to-json-net.aspx\">http://james.newtonking.com/archive/2009/07/10/ado-net-entity-framework-support-accidently-added-to-json-net.aspx

<一个href=\"http://james.newtonking.com/projects/json-net.aspx\">http://james.newtonking.com/projects/json-net.aspx

Just as mentioned in this post, I am getting a Json serialization error while serializing an Entity Framework Proxy:

A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.PurchaseOrder_446B939192F161CDBC740067F174F7A6059B0F9C0EEE68CD3EBBD63CF9AF5BD0'.

But the difference is, I don't have a circular reference in my entities, and it only occurs in our production environment. Locally everything works fine...

My Entities:

public interface IEntity
{
    Guid UniqueId { get; }
    int Id { get; }
} 

public class Entity : IEntity
{
    public int Id { get; set; }
    public Guid UniqueId { get; set; }
}

public class PurchaseOrder : Entity
{
    public string Username { get; set; }
    public string Company { get; set; }

    public string SupplierId { get; set; }
    public string SupplierName { get; set; }

    public virtual ICollection<PurchaseOrderLine> Lines { get; set; }
}

public class PurchaseOrderLine : Entity
{
    public string Code { get; set; }
    public string Name { get; set; }
    public decimal Quantity { get; set; }
}

The GetCurrent action on my PurchaseOrderController throwing the exception:

public class PurchaseOrderController : Controller
{
    private readonly IUnitOfWork _unitOfWork;

    public PurchaseOrderController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public JsonResult GetCurrent()
    {
        return Json(EnsurePurchaseOrder(), JsonRequestBehavior.AllowGet);
    }

    private PurchaseOrder EnsurePurchaseOrder()
    {
        var company = RouteData.GetRequiredString("company");
        var repository = _unitOfWork.GetRepository<PurchaseOrder>();

        var purchaseOrder = repository
                .Include(p => p.Lines)
                .FirstOrDefault
                (
                    p => p.Company == company && 
                         p.Username == User.Identity.Name
                );

        if (purchaseOrder == null)
        {
            purchaseOrder = repository.Create();
            purchaseOrder.UniqueId = Guid.NewGuid();
            purchaseOrder.Company = company;
            purchaseOrder.Username = User.Identity.Name;
            _unitOfWork.SaveChanges();
        }

        return purchaseOrder;
    }
}

解决方案

Your POCO entities are perfectly serializable. Your problem is that the dynamic proxies the EF runtime creates for you are usually not. You can set the context.Configuration.ProxyCreationEnabled to false but then you lose lazy loading. My strong recommendation to you is to use Json.NET which supports serialization for EF entities:

http://james.newtonking.com/archive/2009/07/10/ado-net-entity-framework-support-accidently-added-to-json-net.aspx

http://james.newtonking.com/projects/json-net.aspx

这篇关于循环引用检测异常,而对象序列化到JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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