简体   繁体   English

正在检测字符串的长度超过在服务器上的maxJsonLength属性上设置的值

[英]Detecting The length of the string exceeds the value set on the maxJsonLength property at the server

First let me start by saying I know what this error means. 首先,我先说一下我知道此错误的含义。 I know about <jsonSerialization maxJsonLength="50000000"/> 我知道<jsonSerialization maxJsonLength="50000000"/> And I understand the (in my opinion, very good) reasons for having a limit. 而且我了解(有很好的理由)有限制的原因。

So I am calling a webservice from javascript using the .NET ScriptManager to create all the client code for me. 因此,我正在使用.NET ScriptManager从javascript调用网络服务来为我创建所有客户端代码。

The webservice gets's a bunch of data from SQL, turns it into an array of objects then returns this array. Web服务从SQL获取大量数据,将其转换为对象数组,然后返回此数组。

Under the hood the .NET engine turns this array into a JSON array for sending to the client. 在后台,.NET引擎将该数组转换为JSON数组以发送给客户端。

Sometimes however the resulting JSON string is too long and it throws an exception. 但是,有时生成的JSON字符串过长,并且会引发异常。

I would like some generic way of detecting this error and handling it gracefully (eg by sending some of the data with an indicator that another subsequent request should be made to get the rest) 我想要一种检测此错误并优雅地处理它的通用方法(例如,通过发送一些数据并带有指示应该发出另一个后续请求以获取其余信息的指示符)

What I do not want to do is set a sky high limit on the length of the json string as this could result in it taking a very long time before the client gets all the data. 我不想做的是在json字符串的长度上设置一个天空上限,因为这可能导致它花费很长时间才能使客户端获取所有数据。 I believe the upper limit on this setting to be 2147483644 ie 2GB. 我相信此设置的上限为2147483644,即2GB。 Chances are the client has got bored and wandered off by the time all that data arrives just in time to crash the javascript engine when it tries to parse it. 当所有数据及时到达时,客户端可能会感到无聊和徘徊,这恰恰是在试图解析JavaScript引擎时使其崩溃。

So I guess really what I am after is a way to "know" how long the resulting json string will be before returning it so that it can be truncated as appropriate, or some way to handle the resulting error, shorten the array and try again (and keep shortening and retrying until it works or the array is empty). 所以我想我真正想的是一种“知道”返回的json字符串将持续多长时间的方法,以便可以将其适当地截断,或者采用某种方式来处理所产生的错误,缩短数组并重试(并继续缩短并重试,直到它起作用或该数组为空为止)。

I have tried putting a try/catch around the return statement but this didn't work - I suspect because the webservice method is successfully returning the data to the framework but the framework then chokes on the data. 我曾尝试在return语句周围放置try / catch,但这没有用-我怀疑是因为webservice方法已成功将数据返回到框架,但框架随后使数据阻塞。

I would prefer a solution that doesn't involve returning a string, but if there is no other way I can put up with that. 我希望不涉及返回字符串的解决方案,但是如果没有其他方法,我可以忍受。

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public MyClass[] GetMyClasses()
{
  List<MyClass> lst;
  //code that populate lst with enough instances to break the JSON.
  return lst.ToArray();
}

Instead of trying to figure out the resulting JSON length and then apply some sort of paging, maybe you should always page your data..? 与其尝试找出最终的JSON长度然后进行某种分页,不如应该始终分页数据。

You already know that the method can result in huge data, so why not change your method to GetMyClasses(int skip, int take) to enforce chunks of data of reasonable sizes. 您已经知道该方法会产生大量数据,因此为什么不将您的方法更改为GetMyClasses(int skip,int take)来强制执行合理大小的数据块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字符串的长度超过maxJsonLength属性上设置的值 - length of the string exceeds the value set on the maxJsonLength property 字符串的长度超过maxJsonLength属性c上设置的值。 - The length of the string exceeds the value set on the maxJsonLength property.c# 字符串的长度超过了 mvc 中 maxjsonlength 属性上设置的值 - the length of the string exceeds the value set on the maxjsonlength property in mvc MVC4错误:字符串的长度超过了在maxJsonLength属性上设置的值 - MVC4 error: The length of the string exceeds the value set on the maxJsonLength property 我收到“字符串的长度超过 maxJsonLength 属性上设置的值” - I am getting a “The length of the string exceeds the value set on the maxJsonLength property” 错误“字符串的长度超过了 maxJsonLength 属性上设置的值。” - Error 'The length of the string exceeds the value set on the maxJsonLength property.' 尝试将部分视图转换为字符串时,“字符串的长度超出了在maxJsonLength属性上设置的值。” - “The length of the string exceeds the value set on the maxJsonLength property.” when trying to convert a partial view to string ASP.Net WebForm c#Web服务-字符串的长度超过在maxJsonLength属性上设置的值 - ASP.Net WebForm c# WebService - The length of the string exceeds the value set on the maxJsonLength property 使用JSON JavaScriptSerializer时发生错误“字符串的长度超过了在maxJsonLength属性上设置的值” - Error when using JSON JavaScriptSerializer “The length of the string exceeds the value set on the maxJsonLength property” JavaScript中View转换ViewBag.JsonData时,得到 The length of the string exceeds the value set on the maxJsonLength property - While converting ViewBag.JsonData from View in JavaScript, getting The length of the string exceeds the value set on the maxJsonLength property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM