Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to get the result from my wcf service in json as you can see :

public List<InquiryStatus> SearchInquiryStatus(string userid, string datestart, string dateend)
{
    string result = "";
    using (WebClient ClientRequest = new WebClient())
    {
        ClientRequest.Headers["Content-type"] = "application/json";
        ClientRequest.Encoding = System.Text.Encoding.UTF8;
        result = ClientRequest.DownloadString(ServiceHostName + "/MonitoringInquiryService.svc/SearchInquiryStatus/" +
                                                userid + "/" 
                                                + datestart + "/" + dateend
                                                );
    }
    var javascriptserializer = new JavaScriptSerializer();
    return javascriptserializer.Deserialize<List<InquiryStatus>>(result);
}

But i get this error :

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Parameter name: input

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Parameter name: input

As a note when i call my service url http://reporting.demo.ir/MonitoringInquiryService.svc/SearchInquiryStatus/null/'20171106'/'20171113' in browser i get the result . I googled my error and i found this :

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

I added this to my UI web config .but i get same error.

like image 272
Ehsan Akbar Avatar asked Oct 27 '25 03:10

Ehsan Akbar


1 Answers

Your UI project seems to be in ASP.NET MVC framework where JavaScriptSerializer MaxJsonLength cannot be read from web.config setting as you've specified in the question.

For setting MaxJsonLength, you need to specify value in the code in following way :

var javascriptserializer = new JavaScriptSerializer()
{
    MaxJsonLength = Int32.MaxValue // specify length as per your business requirement
};
like image 200
Akash KC Avatar answered Oct 29 '25 17:10

Akash KC



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!