Note that there are some explanatory texts on larger screens.

plurals
  1. PODetecting The length of the string exceeds the value set on the maxJsonLength property at the server
    primarykey
    data
    text
    <p>First let me start by saying I know what this error means. I know about <code>&amp;lt;jsonSerialization maxJsonLength="50000000"/&amp;gt;</code> And I understand the (in my opinion, very good) reasons for having a limit.</p> <p>So I am calling a webservice from javascript using the .NET ScriptManager to create all the client code for me.</p> <p>The webservice gets's a bunch of data from SQL, turns it into an array of objects then returns this array.</p> <p>Under the hood the .NET engine turns this array into a JSON array for sending to the client.</p> <p>Sometimes however the resulting JSON string is too long and it throws an exception.</p> <p>I would like some generic way of detecting this error and handling it gracefully (e.g. by sending some of the data with an indicator that another subsequent request should be made to get the rest)</p> <p>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. I believe the upper limit on this setting to be 2147483644 i.e. 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.</p> <p>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).</p> <p>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.</p> <p>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.</p> <pre><code>[WebMethod] [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)] public MyClass[] GetMyClasses() { List&lt;MyClass&gt; lst; //code that populate lst with enough instances to break the JSON. return lst.ToArray(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload