【发布时间】:2018-04-25 17:11:06
【问题描述】:
如您所见,我正在尝试从我的 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 网络配置中。但我得到了同样的错误。
【问题讨论】:
-
一次尝试使用代码
javascriptserializer.MaxJsonLength = Int32.MaxValue;中的内联MaxJsonLength设置 -
@AkashKC 它有效。谢谢。为什么?
-
我怀疑,你在控制器中反序列化你的 json,它没有从 web.config 中的 webservices 设置中获取价值。 Web.config jsonSerialization 设置仅由 Web 服务使用。
-
恕我直言,您的
json string长度大于50000000。它在int32.MaxValue的情况下有效,因为int32.MaxValue等于2147483647。要验证这一点,请尝试在web config中将50000000替换为2147483647,看看它是否有效。 -
@EhsanAkbar :该设置仅适用于 Web 服务项目,不适用于 UI 项目。
标签: c# json wcf json-deserialization