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.WrappedAsyncResult
1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 System.Web.Mvc.Async.WrappedAsyncResultBase
1.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.WrappedAsyncResultBase
1.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.WrappedAsyncResultBase
1.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.WrappedAsyncResultBase
1.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)