I was trying trying to serialize a domain model and ran into an issue where I need to convert a dynamic proxy into a POCO. The issue I ran into was that circular references exist by way of virtual properties in the model. Although I attempted to use [ScriptIgnore]
in order to have the serializer not parse those properties, it still does. I believe that this is because the objects are dynamic proxies and there is still some remnants in the properties which cause the parser to enter (which in turn causes a recursion error "circular reference" - I tried limiting the recursion to 3 steps but I got an error of "Recursive steps exceeded").
How can I convert an object from a dynamic proxy to a POCO so that it can be serialized?
Edit: Simple example
public class One : BaseViewModel
{
public int OneId { get; set; }
public virtual ICollection<Two> Two { get; set; }
}
public class Two
{
public int TwoId { get; set; }
public int OneId { get; set; }
[ScriptIgnore]
public virtual One One { get; set; }
}
public abstract class BaseViewModel
{
public string AsJson()
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(this);
}
}