System.InvalidOperationException– Maximum length exceeded

By | November 25, 2008

Problem

You may receiving this error when you try to use javascript to call web methods defined in ASP.Net Web Service file (.ASMX). This error is usually faced when making javascript calls using ASP.Net AJAX framework.

The server method failed with the following error: System.InvalidOperationException-- Maximum length exceeded.

Cause

When you call ASP.Net Web Methods from JavaScript, it uses serialization and de-serialization of returned objects. Serialization converts .Net objects to XML format. When this XML data length exceeds default limit of serialization, ASP.Net throws this error. To prevent this error you should make sure that returned object does not generate large XML data. If it’s not possible to decrease data, you can use below solution. This solution will increase maximum amount of data(length) that can be returned by a web service call. However best practice is to reduce amount of data(or serialized object) that is returned. Otherwise it may slow down ASP.Net application as it has to transmit so much of data for each call.

Solution

In order to resolve this we need to increase maximum length of serialized object that can be returned by web method. This setting can be defined in ASP.Net web.config file. Go to Web.config file of your ASP.Net application. Add following sections if they don’t exist under <configuration>. Please note that you may need to change version number for system.web.extensins library as per your local version.

<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup,  System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,  PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection,  System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup,  System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection,  System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection,  System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection,  System.Web.Extensions, Version=1.0.61025.0,  Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>

<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="5000000">
<converters>
</converters>
</jsonSerialization>
</scripting>
</system.web.extensions>
</configuration>

As you can see above I have set maxJsonLength=”5000000″ in jsonSerialization section. This solves maximum length exceeded error.

Leave a Reply

Your email address will not be published.