Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

length of the string exceeds the value set on the maxJsonLength property

I have a .Net Web service (.asmx) which will return a Json string to my client. However, some of my data is really large and I get this error occasionally.

The length of the string exceeds the value set on the maxJsonLength property.

I've changed the maxJsonLength property to 2147483644, but it still doesn't work. Please help... Thank you.

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



[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public void GetData(string login)
        {
            // throw an error on this line...
            string result = new JavaScriptSerializer().Serialize(service.GetData(login));


            Context.Response.Write(result);
        }
like image 876
George Huang Avatar asked Jan 28 '15 21:01

George Huang


1 Answers

Thanks to Ed Gibbs and @NextInLine 's suggestion. I did the fix as below and it work like a charm now. I also removed the "system.web.extensions" portion away from my web.config

[WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public void GetData(string login)
        {

            // when the amount of data return is huge
            var serializer = new JavaScriptSerializer();

            // we need to do this.
            serializer.MaxJsonLength = Int32.MaxValue;


            var result = serializer.Serialize(service.GetData(login));


            Context.Response.Write(result);
        }
like image 108
George Huang Avatar answered Nov 14 '22 23:11

George Huang