4

The error I'm getting seems to be coming out of the Structs.tt file. I'm using the Northwind db and only using the Products table (I excluded all other tables). I return Json(Product.All()).

Here's the error:

A circular reference was detected while serializing an object of type 'SubSonic.Schema.DatabaseColumn'.Here's the Stack Trace:

System.InvalidOperationException was unhandled by user code Message="A circular reference was detected while serializing an object of type 'SubSonic.Schema.DatabaseColumn'." Source="System.Web.Extensions" StackTrace: at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeEnumerable(IEnumerable enumerable, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat) at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj) at System.Web.Mvc.JsonResult.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.b__e() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass11.<>c__DisplayClass13.b__10() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext) at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext) at System.Web.Mvc.MvcHttpHandler.VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext) at System.Web.Routing.UrlRoutingHandler.ProcessRequest(HttpContextBase httpContext) at System.Web.Routing.UrlRoutingHandler.ProcessRequest(HttpContext httpContext) at System.Web.Routing.UrlRoutingHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext context) at ExtJSNorthwind._Default.Page_Load(Object sender, EventArgs e) in C:\Applications\Spikes\ExtJSNorthwind\ExtJSNorthwind\Default.aspx.cs:line 18 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) InnerException:

Chandu
  • 81,493
  • 19
  • 133
  • 134
kntcnrg
  • 603
  • 3
  • 10
  • 17
  • This is the message of the exception: A circular reference was detected while serializing an object of type 'SubSonic.Schema.DatabaseColumn'. – kntcnrg Jul 28 '09 at 12:51
  • Don't know if this will help to solve the problem, but as I dug deeper into the exception I ran into this: Message: "Method may only be called on a Type for which Type.IsGenericParameter is true." FullName: System.Web.Script.Serialization.JavaScriptSerializer – kntcnrg Jul 28 '09 at 13:06
  • This sounds very similar to http://stackoverflow.com/questions/1153385/a-circular-reference-was-detected-while-serializing-an-object-of-type-subsonic-s – kevinw Jul 28 '09 at 14:59
  • Reading that post the answer was to select specific fields to return. My problem is I need the ProductID and the code fails when it reaches the GetKeyColumn() method in ActiveRecord.cs where it returns the ProductID. The breakpoints on the Structs.cs for the ProductID column are passing. – kntcnrg Jul 28 '09 at 15:40
  • Sound the same as my issue I assumed the circular reference was the fact that we have IQueryable foreign key references and it was stuck in a loop when serializing? I changed it to return only specific columns and it worked. – Jon Jul 29 '09 at 10:50

2 Answers2

16

This is fairly easy to fix. You need to just add a ScriptIgnore attribute to a couple of the methods in the ActiveRecord.tt file. Below are the changes that I made.

        [ScriptIgnore]
    public IList<IColumn> Columns{
        get{
            return tbl.Columns;
        }
    }

Note: I also added the attribute to the following method though I don't know if it is needed in your situation

        [ScriptIgnore]
    public IQueryable<<#=fk.OtherClass #>> <#=propName #>
    {
        get
        {

              var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
              return from items in repo.GetAll()
                   where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
                   select items;
        }
    }
runxc1 Bret Ferrier
  • 8,096
  • 14
  • 61
  • 100
  • 2
    ScriptIgnore attibute is in System.Web.Extensions this is definitely the best solution if you want to serializate the whole object, but I came to realize that when using JSon you won't often use the whole object. Still I'm going to use this for sure. – freddoo Oct 27 '09 at 07:27
  • Thanks! Although I hope there's a better solution this is an awesome _temporary_ fix. – jebcrum Nov 04 '09 at 23:48
  • @woopstash I think the "better" solution is probably to use a DTO or other object to serialize it. In my case, I have 3 properties and I just need to [ScriptIgnore] the one that refers back to the parent... so this is a perfectly reasonable solution for me – PandaWood Mar 03 '14 at 23:12
  • @runxc1 Bret Ferrier : where to place right location for [ScriptIgnore] – Ghanshyam Lakhani Apr 28 '17 at 16:43
5

Seems we have an issue with Serialization :). Can you please add this as an Issue on our list? I'd really appreciate it.