Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScriptSerializer maxJsonLength exeeded

I am creating a web service in c# in visual studio 2013. I am connected to a database and using the following code for returning json.

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetCustomer()
{
    var json = "";
    var customer = from result in dc.Auto_Kada_SIA_Customers
                   select result;

    JavaScriptSerializer jss = new JavaScriptSerializer();
    jss.MaxJsonLength = Int32.MaxValue;
    json = jss.Serialize(customer);
    int t = json.Length;
    return json;
}

but when i try to use it i get the following error

{
  "Message": "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.",
  "StackTrace": "   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\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)",
  "ExceptionType": "System.InvalidOperationException"
}

I would be ok if that is the case but the MaxJsonLentgh is set to 2,147,483,647 and the json.Length is 21,460,284.

What is the problem and how can i fix it?

like image 272
HellOfACode Avatar asked Apr 06 '26 12:04

HellOfACode


1 Answers

Try configuring the max length in web.config as follows

<configuration>  
   <system.web.extensions>
   <scripting>
       <webServices>
           <!-- Update this value to set the max length -->
           <jsonSerialization maxJsonLength="2147483647" />
       </webServices>
   </scripting>
   </system.web.extensions>
</configuration>  
like image 121
Don Avatar answered Apr 09 '26 02:04

Don



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!