HellOfACode
HellOfACode

Reputation: 1743

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?

Upvotes: 3

Views: 15093

Answers (1)

Don
Don

Reputation: 6882

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>  

Upvotes: 11

Related Questions