How to fix the server method failed Maximum length exceeded error when using ASP.NET AJAX

by APIJunkie 14. March 2008 01:21

If you are using ASP.NET AJAX to create RIA's (Rich Internet Applications) you might run into the maximum length exceeded error.

This error occurs when you are trying to send too much data using ASP.NET AJAX from the client to the server and vice versa.

On the function call stack level this error occurs at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize or at System.Web.Script.Services.RestHandler.InvokeMethod depending if you are sending or receiving data.

The problem is that the default maximum size (102400 characters) does not suffice when you try to transfer a significant amount of data.

To increase the maximum allowed send/receive data you need to change the MaxJsonLength Property.

To change the MaxJsonLength Property you can edit your web.config file.

Look for the following commented section in the web.config file:

 

<!--
Uncomment this line to customize maxJsonLength and add a custom converter -->

<!--

<jsonSerialization maxJsonLength="500">

<converters>

<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>

</converters>

</jsonSerialization>

-->

 

Convert it to something along the following lines (see note below):

 

<!--
Uncomment this line to customize maxJsonLength and add a custom converter -->

<jsonSerialization maxJsonLength="200000"><

converters>

<!--<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>--></

converters>

</jsonSerialization>

 

Note that in the example above the max size was set to 200,000 characters.

You should calculate and test the size you will need for your data (there might be a few bytes of overhead so take that into account).

 

Tags:

.NET | AJAX | PRB | JSON | How To

Avoiding some common errors when consuming web services and calling page methods using ASP.NET AJAX extensions

by APIJunkie 13. February 2008 10:14

I have compiled a small list of things to remember in order to avoid some common errors when using ASP.NET AJAX extensions.

1. Remember to declare your web services using the ScriptService attribute.

Why bother?

If you forget to do that you will not be able to easily access the web service from the client side script. You will be missing the automatically generated client script that will allow you to call the web service from your client script code.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
   ...

}

}

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/T_System_Web_Script_Services_ScriptServiceAttribute.aspx


2. If you wish to access session state in your web service functions remember to set the EnableSession attribute to true in your web method declaration.

Why bother?

If you forget to do this you will not be able to access session variables inside your web service methods.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
    [System.Web.Services.WebMethod(EnableSession=true)]

    public void myFunc()
    {
       Session["XVAR"] = 1;
    }

}

}

More Info:

http://msdn2.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx

 

3. If you wish to call static page methods from client script remember to set the EnablePageMethods property of the script manager to true.
 

Why ?

If you forget to do that you will not be able to access the static page methods from the client side script.

Example:

<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

 

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/P_System_Web_UI_ScriptManager_EnablePageMethods.aspx

 

4. If you wish to mix json.js client side script with the AJAX .NET client side script, use the script manager/script manager proxy Scripts property.

Why ?

You might receive a client side script error.

Example:

ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

For more info you can read my previous post on the subject -> How to use json.js client side JavaScript file inside an AJAX.NET project

 

Tags:

.NET | AJAX | Troubleshoot | JSON | JavaScript

How to use json.js client side JavaScript file inside an AJAX.NET project

by APIJunkie 11. February 2008 08:12

Sometimes it is necessary to mix json.js functions like toJSONString() with client script generated by the AJAX.NET library.

If you include the json.js file in your client script you might receive enumValueNotInteger error.

A little digging around landed me on the following helpful discussion at http://forums.asp.net/t/1077290.aspx

Steve Marx raises several options to solve the problem in the above discussion.

To solve my problem I used the script manager Scripts property option.

Example:

<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

Note if you are using master pages or need json.js script inside custom controls you can use the script manager proxy Scripts property instead.

Example:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</asp:ScriptManagerProxy>

Tags:

.NET | AJAX | JavaScript | JSON

About the author

Name of author

I was first wounded by x86 assembly, recovered and moved on to C. Following a long addiction to C++ and a short stint at rehab I decided to switch to a healthier addiction so I am now happily sniffing .NET and getting hooked on Silverlight.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

  James Bacon