0

I am making a jQuery AJAX call to a back-end WebMethod in an aspx.cs page. I am getting an error in the .NET JSON serialization. As such, I am looking for ways to either fix the error or avoid using JSON (the only return format for WebMethods):

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property

The associated StackTrace is : at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary'2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

The backend code is as follows (note: result is, in actuality about 110k of a control rendered to a string):

[WebMethod]
public static string GetContactListControl()
{
    try
    {
        var result = "Hello World!"
        return result;
    }
    catch (Exception e){
        Logging.LogException(e);
        return "Exception Thrown";
    }
}

And I am never hitting the catch block, which to me shows that this issue is outside of my code.

I found a fix involving changing the web.config by inserting the following block, but it is not working:

<system.web.extensions>
    <scripting>
       <webServices>
          <jsonSerialization maxJsonLength="123456"></jsonSerialization>
       </webServices>
    </scripting>
</system.web.extensions>

Project is .NET 3.5.

Thank you for any ideas and suggestions!

madth3
  • 7,275
  • 12
  • 50
  • 74
  • I'm assuming you meant that a breakpoint at the beginning of the method was never hit when you say that it "never hits the `catch` block". With the code as-is, I sincerely doubt that `return "Hello World!"` could ever possibly throw an exception. – Chris Sinclair Jul 05 '12 at 20:06
  • Try increasing the `maxJsonLength` by a tonne more. About 110kb is about 112,640 bytes which isn't too far from 123,456 bytes. Perhaps additional overhead (or the "about" isn't too exact) is breaking the limit. Try punching it up to 9999999 and see what happens. – Chris Sinclair Jul 05 '12 at 20:09
  • Correct about the breakpoint being fine. When I break on _return_ _result_, it shows the value of result without any issue. – WhistlingZebra Jul 05 '12 at 20:10
  • Thanks for the suggestion, Chris, but using the Web.Config complains about being invalid and a search for the IIS error being returned makes it appear that the fix is only .NET 4.0. Thanks to L.B. also! But due to project restrictions, it will be difficult to get approval for open source solutions. – WhistlingZebra Jul 05 '12 at 20:14
  • IIS Error mentioned above: `HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid.` – WhistlingZebra Jul 05 '12 at 20:16
  • @WhistlingZebra that's odd that the fix is only for .NET 4.0. The .NET 3.5 version of this page seems to indicate that it's available for 3.5: http://msdn.microsoft.com/en-us/library/bb763183%28VS.90%29.aspx – Chris Sinclair Jul 05 '12 at 20:26
  • I'm going to go out on a limb here: have you tried making the `` self-closing? :-S – Chris Sinclair Jul 05 '12 at 20:29
  • @ChrisSinclair I looked at that MSDN article you sent over and it put me on the right track (since I was obviously on the wrong one... lol). Turns out there was an issue with the assemblies listed in the web.config file. My misdiagnosis definitely had me looking in the wrong areas! Thanks for your help!! – WhistlingZebra Jul 06 '12 at 14:00
  • @WhistlingZebra Good to hear! :D – Chris Sinclair Jul 06 '12 at 14:16
  • an "issue with the assemblies" doesn't really help out people like me searching for a solution... – Sean Kendle Aug 26 '15 at 20:30

1 Answers1

2

Configuration of the maxJsonLength property must be setup in the web.config. In order to allow this configuration to be permitted by IIS, the following <sectionGroup> must be included inside of <configSections>:

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