1

I have a ASP.NET project, with some AJAX calling a webmethod, that would return JSON.

If the DataSet has about 100 lines, there is no problem. But with 1000 lines, it launch the error:

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

    [WebMethod(EnableSession = true)]
    public static string PublicWebMethod()
    {
            DataSet ds = new DataSet(); // in the reality do a mountrous query

            if (ds.Tables[0].Rows.Count > 0)
            {
                return JsonConvert.SerializeObject(clsUtil.ToArray(ds.Tables[0]));
            }
            else
            {
                return "false";
            }
        }
    }

How can I solve this by setting a configuration in the method?

I don't want to change the web.config

Lucas Anschau
  • 91
  • 1
  • 9
  • 2
    Maybe not use DatSet and JsonConvert.Serialize, but use DataReader and JsonTextWriter so you can write JSON in a streaming fashion. A tad bit more code, I know, but more performant and not so heavy on the memory requirements. – Adam B Dec 06 '18 at 18:05
  • @AdamB This is a good workaround. – Lucas Anschau Dec 06 '18 at 21:53

1 Answers1

2

I had the similar issue and took a while to figure out the problem and fix. Please include the following code after

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="50000000" />
    </webServices>
  </scripting>
</system.web.extensions>
Sam
  • 21
  • 1