字符串的长度超过了 maxJsonLength 属性上设置的值。c#

Ehs*_*bar 4 c# wcf json json-deserialization

我正在尝试从我的 wcf 服务中获取 json 格式的结果,如您所见:

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);
}
Run Code Online (Sandbox Code Playgroud)

但我收到这个错误:

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
Run Code Online (Sandbox Code Playgroud)

请注意,当我http://reporting.demo.ir/MonitoringInquiryService.svc/SearchInquiryStatus/null/'20171106'/'20171113' 在浏览器中调用我的服务网址时,我得到了结果。我用谷歌搜索了我的错误,发现了这个:

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 
Run Code Online (Sandbox Code Playgroud)

我将其添加到我的 UI Web 配置中。但我遇到了同样的错误。

Aka*_* KC 5

您的 UI 项目似乎位于 ASP.NET MVC 框架中,JavaScriptSerializer MaxJsonLength无法从您在问题中指定的 web.config 设置中读取。

对于设置MaxJsonLength,您需要按以下方式在代码中指定值:

var javascriptserializer = new JavaScriptSerializer()
{
    MaxJsonLength = Int32.MaxValue // specify length as per your business requirement
};
Run Code Online (Sandbox Code Playgroud)