13

I'm having problems with a circular reference when i try and serialise an object returned via EF4 CTP5. Im using the code first approach and simple poco's for my model.

I have added [ScriptIgnore] attributes to any properties that provide a back references to an object and annoyingly every seems to work fine if i manually instantiate the poco's, i.e. they serialise to JSON fine, and the scriptignore attribute is acknowledged. However when i try and serialise an object returned from the DAL i get the circular reference exception "A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.xxxx'"

I have tried several ways of retreiving the data but they all get stuck with this error:

    public JsonResult GetTimeSlot(int id) {
        TimeSlotDao tsDao = new TimeSlotDao();
        TimeSlot ts = tsDao.GetById(id);
        return Json(ts);
    }

The method below works slightly better as rather than the timeslot dynamic proxied object causing the circular refference its the appointment object.

    public JsonResult GetTimeSlot(int id) {
        TimeSlotDao tsDao = new TimeSlotDao();
            var ts = from t in tsDao.GetQueryable()
                 where t.Id == id
                 select new {t.Id, t.StartTime, t.Available, t.Appointment};
        return Json(ts);
    }

Any ideas or solutions to this problem?

Update I would prefer to use the out of the box serialiser if possible although Json.Net via nuget is ok as an alternative i would hope its possible to use it as I intended as well...

1

5 Answers 5

4

I had a similar problem with an IIS hosted WCF service and trying to serialize POCO objects with the DataContractJsonSerializer class. The built in JSON serializer does not seem to handle circular references at all. I was able to get around it by handling the serialization myself using the JSON.net serializer, and just returning json strings from my methods. The JSON.net serializer has an option to ignore circular references as json itself does not support them.

1
  • +1 use the Newtonsoft for all things JSON. Bonus points to install the dependency to it through NuGet since the OP mentioned using MVC3. Jan 5, 2011 at 20:35
2

No matter what I did the dynamic proxies kept being a sticking point, I went as far as removing all circular references in my model! but still the problem persisted.

I tried Json.Net but the same problem occurred.

In the end I stumbled upon a post about using a custom JavaScriptConverter

http://hellowebapps.com/2010-09-26/producing-json-from-entity-framework-4-0-generated-classes/

Implemented the code and bobs your uncle everything worked

2
  • I found that the code on that post didn't compile, and when i edited it enough to get it to compile, it still didn't work. Could you post your working custom JavaScriptConverter class here?
    – codemonkey
    Aug 4, 2011 at 19:24
  • @TechnicalSmile use json.net a lot has changed since i posted this question, seeing as it is now the default json serializer in the mvc stack, also part of the problem was the CTP version of EF4 i was using at the time as it was pre release.
    – nakchak
    Feb 4, 2014 at 13:47
1

I solved this without having to resort to an external JSON serializer. In a nutshull I disabled ProxyCreation in the constructor of my object context.

I am not sure why this works but I posted a follow up question here.

1
  • 1
    I didnt want to disable lazy loading to get this to work. Annoyingly the ScriptIgnore Attribute doesn't appear to be picked up by the EF4 CTP5 proxy generation code. So when the serializer tries to serialise the proxy object it will try and recurse the relationships. This problem can be mostly mitigated by using anonymous objects to serialise to JSON as most of my problems were being caused by trying to return too full an object graph.
    – nakchak
    Feb 3, 2011 at 15:44
1

I used the following ContractResolver. Note that I inherited from the CamelCaseContractPropertyResolver to get that feature as well, but you can also inherit directly from DefaultContractResolver.

using System;
using System.Collections.Generic;
using System.Reflection;
using Newtonsoft.Json.Serialization;

namespace MyNamespace
{
    /// <summary>
    /// This class enables EntityFramework POCO objects to be serialized. In some cases POCO
    /// objects are subclassed by a proxy which has an additional member _entityWrapper. This
    /// object prevents serialization (circular references and references to non-serializable types).
    /// This removes the _entityWrapper from the list of members to be serialized.
    /// </summary>
    public class ContractResolver : CamelCasePropertyNamesContractResolver
    {
        protected override List<MemberInfo> GetSerializableMembers(Type objectType)
        {
            if (objectType.FullName.StartsWith("System.Data.Entity.DynamicProxies."))
            {
                var members = base.GetSerializableMembers(objectType);
                members.RemoveAll(memberInfo => memberInfo.Name == "_entityWrapper");
                return members;
            }
            return base.GetSerializableMembers(objectType);
        }
    }
}

To use it, create your serializer and then set the ContractResolver property to a new instance of this class:

var ser = JsonSerializer.Create(sJsonSerializerSettings);            
ser.ContractResolver = new ContractResolver(); 
0

I have met this issue too. Answers to this topic contains number solutions. But best different solutions for different cases with explanation and moreover without custom serializes I have found in the article by Hongye Sun - Loop Reference handling in Web API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.