System.InvalidOperationException-- Maximum length exceeded

Problem

You get following error when you use javascript to call Web Methods defined in Web Service file(ASMX). This usually happens when you are using ASP.Net AJAX framework.

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

Everything works fine until the web service returns a larger number of objects.

Solution

Go to your Web.Config file. Add following sections if they don't exist under <configuration>

<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.

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 above solution to fix it. But this may slow down ASP.Net application as it has to transmit so much of data for each call.