6

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);
        }
mason
  • 31,774
  • 10
  • 77
  • 121
George Huang
  • 2,504
  • 5
  • 25
  • 47
  • Does [this question (and its answers)](http://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config) resolve your issue? – NextInLine Jan 28 '15 at 21:29
  • George - the second answer in the link from @NextInLine is the one you want: set `maxJsonLength` as a property on your `new JavaScriptSerializer`. The first answer in the link won't help you. – Ed Gibbs Jan 28 '15 at 21:33
  • You say you are using ["Newtonsoft.Json"](http://www.newtonsoft.com/json), but in your code you are using [`JavaScriptSerializer`](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer%28v=vs.110%29.aspx). Just to confirm, you're not using Newtonsoft.Json, right? – dbc Jan 28 '15 at 23:09
  • @dbc: Yes you are right. I am not using Newtonsoft Json for this case. I was used it for another method. I am sorry for the confusing. I will update my question. Thank you :-) – George Huang Jan 30 '15 at 14:28
  • @Ed Gibbs: Hi Ed, Thank you. Your suggestion works! I used the second one. – George Huang Jan 30 '15 at 15:21
  • Glad to hear it George. Thanks for following up! – Ed Gibbs Jan 30 '15 at 16:51

1 Answers1

12

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);
        }
George Huang
  • 2,504
  • 5
  • 25
  • 47