c# - 字符串长度超过maxJsonLength属性设置的值.c#

标签 c# json wcf json-deserialization

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

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

但是我得到这个错误:

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

当我调用我的服务 url 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> 

我将其添加到我的 UI 网络配置中。但我得到了同样的错误。

最佳答案

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

要设置MaxJsonLength,您需要通过以下方式在代码中指定值:

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

关于c# - 字符串长度超过maxJsonLength属性设置的值.c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47257227/

相关文章:

json - 应用引擎 - Python : UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 1: invalid continuation byte

c# - 找不到 WS-Security header

c# - Dictionary<Guid, Guid> 从 C# 中的 appsettings.json 映射

c# - Azure IOT 中心 - 监控 1 台设备的设备到云通信

c# - 在 Asp.net Core 1.1 中传递两个参数

c# - 为什么 enum.ToString box/callvirt,而不是推送地址和调用?还有其他特殊情况吗?

java - 解析 JSONArray 时出现 NullPointerException

javascript - ajax返回值json格式问题

c# - 有没有办法将子容器传递到 NserviceBus 管道中?

c# - 用于在客户端动态添加传出消息属性的 WCF 扩展