Resumé

JSON, ASP.NET Ajax and RecursionLimit Exceptions

Cristian Merighi () 0.00

A brief digression about ASP.NET Ajax, JSON and some work experience about the subtle Recursion Limit Exceeded Exception
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.

There are technolgies and design patterns I can't avoid to use/follow: it's a deontological principle, which is extrinsic to the value (let's say the cost) of the project I'm involved in.
Multiple-tier application design is a must since I learned this modus operandi. So plenty of business entities and related pros (problems atomization, centralized decisions, ease of maintenance, ...intellisense!^_^;).

Power to the client! I always agred with (I love Client Side languages and technologies), even when it costs me discussions with those (non-programmers) who prefer Opera to Internet Explorer and sort of amenities like that... ASP.NET AJAX and Silverlight are targeting three main browser families: IE (Windows), FireFox (Windows/Mac) e Safari (Mac), fair enough for anyone who cares about decent browsers.

ASP.NET AJAX was I saying? Yep, thanks to this Client Side Framework it is possible to exploit - at the same time - a structured layered server-side software and the power of your own browser, easing developers' life and giving a more comfortable experience to the end-user.

JSON (Javascript Serialization Object Notation) binds those two world.

ASP.NET Ajax permits to retrieve data from the Client using functions (PageMethods or WebServices in general), getting "complex" objects which carry 'em.

Let's exemplify a scenario through the following diagram:

class diagram

Now we set a PageMethod to retrieve a random Container class instance:

[System.Web.Script.Services.ScriptMethod, System.Web.Services.WebMethod]
public static Container GetContainerInstanceFromDB()
{
    return PacemComp.DAL.Adapter.GetRandomContainer();
}

markup and javascript for the context page are the following:

<asp:ScriptManager runat="server" ID="sm" 
EnablePageMethods="true" />

<script type="text/javascript"/>
//<![CDATA[
    function triggerMethod(){
        PageMethods.GetContainerInstanceFromDB(onCallBackSuccess, onCallBackError);
    }
    
    function onCallBackSuccess(result, userContext, methodName){
        alert(result.DummyProperty); //please not JSON formatted result
    }
    
    function onCallBackError(error, userContext, methodName){
        if(error !== null) {
            alert(error.get_message());
        }
    }
//]]>
</script/>

Everything seems pretty easy and straightforward, it hides besides a subtle insidiousness:
by running this piece of code you'll experience a sly exception: RecursionLimit was exceeded!

What happens? A limit to the amount of recursions is set for a JSON serialization: its default value is 100.

By the way, it can be modified by editing the correspondant web.config tag:

<jsonSerialization maxJsonLength="500"
recursionLimit="1000">
<!-- converters & C... -->
</jsonSerialization>

Back to the exception, it is due to the loop that involves the serializer. First it creates an instance of Container, then an array of Item, but for each of 'em again a new instance of a Container parent element and so on...
That cannot be done - obviously - and the exception gets thrown. To obey that, it's sufficient to remove, for example, the "Items" property from the Container type.

Take care. Bye.

Feedbacks

  • Re: JSON, ASP.NET Ajax and RecursionLimit Exceptions

    Rafael Miranda Friday, July 06, 2007 0.00

    Hi Christian... So, do you mean that I cant have a class like: public class Resource { #region Fields private string name; private Resource child; #endregion #region Properties public string Name { get { return this.name; } set { this.name = value; } } public Resource Child { get { if (this.child == null) this.child = new Resource(); return this.child; } } #endregion } Thank you!

  • Re: JSON, ASP.NET Ajax and RecursionLimit Exceptions

    CMerighi Saturday, July 07, 2007 0.00

    ...Yep (in a standard JSON scenario of course). You could expose the "Child" member as a method (a "GetChild()" one). The JavaScript Serialization process skips method members. Another <i>escamotage</i> could be the exposure of the "Child" property as a base class (let's say "ResourceBase") where only the "Name" property gets exposed. <i>public interface IWithName { string Name { get;} } public class ResourceBase : IWithName { #region IWithName Members public virtual string Name { get { return "My Name"; } } #endregion } public class Resource : ResourceBase { public IWithName Child { get { return new ResourceBase(); } } public Resource GetChild() { return new Resource(); } }</i> Hope it helps.

  • Re: JSON, ASP.NET Ajax and RecursionLimit Exceptions

    Trail Blazer Monday, June 02, 2008 0.00

    I've tried to add &lt;jsonSerialization maxJsonLength="500" recursionLimit="1000"> &lt;!-- converters & C... --> &lt;/jsonSerialization> but i get configuration error (Unknown section) please help

  • Re: JSON, ASP.NET Ajax and RecursionLimit Exceptions

    CMerighi Monday, June 02, 2008 0.00

    Hi TB, looks like you missed to declare the relevant custom section: I copied 'n' pasted the usual custom configSections node of an ASP.NET AJAX enabled website (3.5 version). As you'll see it embeds, among the others, the jsonSerialization declaration: <div style="font-family: Courier New; font-size: 10pt; color: black; background: white;"> <p style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: #a31515;">configSections</span><span style="color: blue;">&gt;</span></p> <p style="margin: 0px;"><span style="color: blue;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515;">sectionGroup</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">system.web.extensions</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35</span>"<span style="color: blue;">&gt;</span></p> <p style="margin: 0px;"><span style="color: blue;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515;">sectionGroup</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">scripting</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35</span>"<span style="color: blue;">&gt;</span></p> <p style="margin: 0px;"><span style="color: blue;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515;">section</span><span style="color: blue;"> </span><span style="color: red;">name</span

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds