Friday, October 19, 2012

System.InvalidOperationException-- Maximum length exceeded

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>
01.<configuration>
02.<configSections>
03.<sectionGroup name="system.web.extensions"
04.type="System.Web.Configuration.SystemWebExtensionsSectionGroup, 
05.System.Web.Extensions, Version=1.0.61025.0, Culture=neutral
06.PublicKeyToken=31bf3856ad364e35">
07.<sectionGroup name="scripting"
08.type="System.Web.Configuration.ScriptingSectionGroup, 
09.System.Web.Extensions, Version=1.0.61025.0, Culture=neutral
10.PublicKeyToken=31bf3856ad364e35">
11.<section name="scriptResourceHandler"
12.type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, 
13.System.Web.Extensions, Version=1.0.61025.0, 
14.Culture=neutral, PublicKeyToken=31bf3856ad364e35"
15.requirePermission="false"
16.allowDefinition="MachineToApplication"/>
17.<sectionGroup name="webServices"
18.type="System.Web.Configuration.ScriptingWebServicesSectionGroup, 
19.System.Web.Extensions, Version=1.0.61025.0, 
20.Culture=neutral, PublicKeyToken=31bf3856ad364e35">
21.<section name="jsonSerialization"
22.type="System.Web.Configuration.ScriptingJsonSerializationSection, 
23.System.Web.Extensions, Version=1.0.61025.0, 
24.Culture=neutral, PublicKeyToken=31bf3856ad364e35"
25.requirePermission="false" allowDefinition="Everywhere" />
26.<section name="profileService"
27.type="System.Web.Configuration.ScriptingProfileServiceSection, 
28.System.Web.Extensions, Version=1.0.61025.0, 
29.Culture=neutral, PublicKeyToken=31bf3856ad364e35"
30.requirePermission="false"
31.allowDefinition="MachineToApplication" />
32.<section name="authenticationService"
33.type="System.Web.Configuration.ScriptingAuthenticationServiceSection, 
34.System.Web.Extensions, Version=1.0.61025.0, 
35.Culture=neutral, PublicKeyToken=31bf3856ad364e35"
36.requirePermission="false"
37.allowDefinition="MachineToApplication" />
38.</sectionGroup>
39.</sectionGroup>
40.</sectionGroup>
41.</configSections>
42.<system.web.extensions>
43.<scripting>
44.<webServices>
45.<jsonSerialization maxJsonLength="5000000">
46.<converters>
47.</converters>
48.</jsonSerialization>
49.</scripting>
50.</system.web.extensions>
51.</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.

No comments:

Post a Comment