Ehsan Akbar
Ehsan Akbar

Reputation: 7301

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.

Upvotes: 4

Views: 8817

Answers (2)

Akash KC
Akash KC

Reputation: 16310

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
};

Upvotes: 5

Koderzzzz
Koderzzzz

Reputation: 869

Add this line in your function.

 var javascriptserializer = new JavaScriptSerializer();
javascriptserializer.MaxJsonLength = Int32.MaxValue;

Upvotes: 3

Related Questions