SubSonic 3.0.0.2 Structs.tt
Asked Answered
S

2

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:

Subsequence answered 28/7, 2009 at 12:44 Comment(5)
This is the message of the exception: A circular reference was detected while serializing an object of type 'SubSonic.Schema.DatabaseColumn'.Subsequence
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.JavaScriptSerializerSubsequence
This sounds very similar to #1153885Redstart
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.Subsequence
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.Monostrophe
U
5

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

Uneasy answered 28/7, 2009 at 16:55 Comment(2)
I posted it to GitHub and it is now issue #82. Thanks Rob.Subsequence
Check out the fix that I posted below. GitHub was as slow as mud or i would have posted it there too.Josiahjosias
J
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;
        }
    }
Josiahjosias answered 16/9, 2009 at 15:5 Comment(4)
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.Slack
Thanks! Although I hope there's a better solution this is an awesome temporary fix.Milkandwater
@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 meThiamine
@runxc1 Bret Ferrier : where to place right location for [ScriptIgnore]Disfrock
U
5

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

Uneasy answered 28/7, 2009 at 16:55 Comment(2)
I posted it to GitHub and it is now issue #82. Thanks Rob.Subsequence
Check out the fix that I posted below. GitHub was as slow as mud or i would have posted it there too.Josiahjosias

© 2022 - 2024 — McMap. All rights reserved.