1

My question is quite similar to this guy's one (which is unanswered), but the difference is that my request is local, so I'm not interested in "alternative solutions for big data".

The error:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

The fact: I'm using an external plugin called Rotativa to create PDF files based on a MVC View. It works by passing as parameter the View's Controller itself, and the images I need to show in the document are all in Base64.

I've set JsonMaxLength not only in web.config but also in JavasScriptSerializer. The request is made using PostAsJsonAsync method from HttpClient.

Web.config

<appSettings>
  <add key="aspnet:UpdatePanelMaxScriptLength" value="2147483644" />
  <add key="aspnet:MaxJsonDeserializerMembers" value="2147483644" />
</appSettings>
<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="2147483644" />        
    </webServices>
  </scripting>
</system.web.extensions>

Code:

JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
System.Net.Http.HttpResponseMessage response = client.PostAsJsonAsync(URL, serializer.Serialize(myJsonData)).Result;

All other solutions that I've found in Stack Overflow uses JsonResult or similar, but none using HttpClient.

Important:

1- I can't use another plugin. Company's orders.

2- I'm sure that the object is smaller than Int32.MaxValue.

System.Text.Encoding.Default.GetByteCount((serializer.Serialize(myJsonData))) equals 20092960 

3- Maybe there's another way, but I'm kind of newbie in this matter.

[EDIT]

Exception and Stack Trace:

[Argument Exception] Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +168 System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext) +211 System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +16 System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext) +69 System.Web.Mvc.ControllerBase.get_ValueProvider() +30 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +62 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743 System.Web.Mvc.Async.WrappedAsyncResult1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343 System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465 System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52 System.Web.Mvc.Async.WrappedAsyncVoid1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 System.Web.Mvc.Async.WrappedAsyncResultBase1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

[UPDATE 16/11]

I was able to use JSON.NET, but none of my tries were successful.

  • JsonValueProviderFactory: made it, but don't seem to change a thing. Don't work neither using JavascriptSerializer nor JsonConvert (JSON.NET). (MAYBE I'm not using it the right way)
  • PushStreamContent (as explained HERE): sends a default instance of MyJsonData object. Just passed it as content for PostAsync method.
  • JsonContent implementation (HERE): "Cannot access a closed Stream" exception (System.IO.__Error.StreamIsClosed()). This happens if I'm not using "await client.PostAsync...". If I use it, it simply vanishes and gives me no response. Idk if this makes any difference.
  • JsonConvert (JSON.NET): same error (maxJsonLength issue).

Any other thoughts? (If someone has some good idea using JSON.NET, please share it. The ones I found weren't useful)

thiagoprzy
  • 411
  • 2
  • 8
  • 21
  • Would it be possible for you to use Json.NET instead of JavaScriptSerializer? Even the documentation on the [JavaScriptSerializer](https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer(v=vs.110).aspx) class recommends this. Also, I'm not sure if this might be helpful: https://stackoverflow.com/questions/41167770/mvc-5-increase-max-json-length-in-post-request/41172517#41172517 – kimbaudi Nov 14 '17 at 17:55
  • @kimbaudi thanks for the comment. I'll check if Json.NET is permitted (the guys here loves a restriction). About the link, it uses JsonResult, so I think it can't help me, unfortunately. – thiagoprzy Nov 14 '17 at 18:55
  • Can you share the fuil `ToString()` output of the exception including the exception type, traceback, message and inner exception? You're serializing the JSON manually after all so it would be useful to know exactly where the exception is thrown. – dbc Nov 14 '17 at 18:58
  • OK, from the [source code for `JsonValueProviderFactory`](https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Mvc/JsonValueProviderFactory.cs) it appears that `JavaScriptSerializer()` is used without modifying `MaxJsonLength`. You may need to create your own custom `ValueProviderFactory` for JSON as outlined in the second half of the answer to [Setting the Default JSON Serializer in ASP.NET MVC](https://stackoverflow.com/q/14591750). – dbc Nov 14 '17 at 21:57
  • Here's a `ValueProviderFactory` I found with a quick search: https://gist.github.com/rorymurphy/db0b02e8267960a0881a. For more see also https://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/ – dbc Nov 14 '17 at 21:57
  • @dbc please check my update :) – thiagoprzy Nov 16 '17 at 19:30
  • @kimbaudi please check my update. I'm able to use JSON.NET, but I'm having issues with it. – thiagoprzy Nov 16 '17 at 19:31

0 Answers0