0

I do not see any error in my traces but when I see error with procdump.exe, I see the error called

[12:09:52] Exception: E0434F4D.System.ArgumentException ("Error during serializa tion or deserialization using the JSON JavaScriptSerializer. The length of the s tring exceeds the value set on the maxJsonLength property.")

Here is my controller:

    public class CommonConverterController : Controller
    {
      // Post
      [System.Web.Http.HttpPost]
      public JsonResult CommonToTypeScriptConverter([FromBody]Foo param)
      {
         string result = string.Empty;
         List<string> resultList = new List<string>();

        try
        {
           ....
                            
           TraceInformation(Common.Enums.TraceEventCategories.None, "CommonToTypeScriptConverter", "fileName: " + fileName);
           ....
           ....
           ....
           
            var jsonResult = Json(resultList, JsonRequestBehavior.AllowGet);
            jsonResult.MaxJsonLength = int.MaxValue;
            return jsonResult;
        }
        catch(Exception ex)
        {
           .....
        }
    }

I tried solutions in Stackoverflow to fix this error. The solutions were about MaxJsonLength. However, I am using JSON serializer at the end of method. There is no trace line about the enterence of method. I cannot catch the error in catch block as well. I saw the error by using procman.

More than 1.5MB file size does not work. Less than 1.5 MB works with no problem.

To sum up, according to my traces, my http.post call cannot be reached inside the method. Is there any solution to fix this error ?

ahmet gül
  • 193
  • 1
  • 11
  • You're right. Sorry for my mistake. I fixed it now. – ahmet gül Mar 29 '22 at 12:25
  • What is `resultList` and why is it so big it exceeds the JSON limit? That's a serious problem to begin with. Loading so much data in memory and serializing it to a string is very slow and eats a lot of CPU and RAM. That's why all HTTP APIs place limits on how many results can be returned at a time and force you to retrieve large result sets in pages – Panagiotis Kanavos Mar 29 '22 at 12:27
  • Well, the input data is just 1.5 mb. Lower than 1.5 mb works with no problem. The result list should be no more than 1.5 mb. They're just strings. For me, the problem is that I cannot see any trace log inside method when it gets error. That's why I believe that resultList cannot be created. – ahmet gül Mar 29 '22 at 12:29
  • That's not "just". Besides, if the *input* is 1.5MB, the request fails before your code executes. You need to change the `maxJsonLength` property in web.config, not the response size – Panagiotis Kanavos Mar 29 '22 at 12:32
  • I set the config file **jsonSerialization maxJsonLength="500000000"** but it did not fix my problem. – ahmet gül Mar 29 '22 at 12:49
  • I also checked the successful one size from network tab. 1.3MB file shows 2.2MB in network tab. Basically, I am not working with huge file size. – ahmet gül Mar 29 '22 at 12:50
  • Your problem is trying to use such a huge request in the first place. Don't do that. Especially not with the obsolete `JavaScriptSerializer`. That class was only meant as a stop-gap measure and performs far worse than the JSON.NET serializer used in Web API, or System.Text.Json in ASP.NET Core – Panagiotis Kanavos Mar 29 '22 at 12:51
  • `I am not working with huge file size.` yes you are. This isn't a file, it's an HTTP request that needs to be deserialized by an obsolete serializer into objects. Your application will end up allocating multiple times that size as buffers and .NET objects that will then have to be garbage-collected. – Panagiotis Kanavos Mar 29 '22 at 12:52
  • I am sorry but I could not understand the solution. Instead of sending 1.5MB post request, what should I do ? – ahmet gül Mar 29 '22 at 12:53
  • Why are you even trying that in the first place? And why are you using MVC to create an HTTP API instead of Web API or ASP.NET Core? `JavaScriptSerializer` was only used by MVC. ASP.NET Web API uses JSON.NET – Panagiotis Kanavos Mar 29 '22 at 12:54
  • In any case, your exact question has already been asked [and answered here](https://stackoverflow.com/questions/1151987/can-i-set-an-unlimited-length-for-maxjsonlength-in-web-config). You didn't post your `web.config` so it's possible you changed the wrong setting. If it looks like a mess, it's because it is - ASP.NET MVC wasn't meant for APIs. – Panagiotis Kanavos Mar 29 '22 at 12:55
  • Which runtime are you using? If you use the (completely unsupported) .NET Framework 3.5 runtime [there was a known bug that ignored the max size](https://support.microsoft.com/en-us/topic/fix-the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property-error-message-when-you-access-an-asp-net-3-5-web-form-b07329a3-052e-e4d2-7578-e171292d34dc). Anything below .NET Framework 4.5.2 has reached End-Of-Life *years* ago though, and even 4.6.1 will reach EOL in a few weeks – Panagiotis Kanavos Mar 29 '22 at 13:00
  • I am using .NET 4.8 – ahmet gül Mar 29 '22 at 13:01
  • Why are you using MVC for an API then? Again, `JavaScriptSerializer` is only used by MVC. Don't use it. Use Web API instead. Or configure MVC to use JSON.NET – Panagiotis Kanavos Mar 29 '22 at 13:02
  • 1
    There's [another duplicate for request limits](https://stackoverflow.com/questions/41167770/mvc-5-increase-max-json-length-in-post-request) and all solutions stink one way or another. The accepted solution reads the `Request` stream directly using JSON.NET. Web API doesn't have this problem – Panagiotis Kanavos Mar 29 '22 at 13:05
  • After I your read your comments, I understand the problem. I will use WebApi libraries instead of MVC which I do not need. Thank you very much for your help. – ahmet gül Mar 29 '22 at 13:40

0 Answers0