JsonValueProviderFactory throws "request too large"
Solution 1
If you use JSON.NET for serialization/deserialization, you could substitute the default JsonValueProviderFactory with a custom one as shown in this blog post:
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory
{
public override IValueProvider GetValueProvider(ControllerContext controllerContext)
{
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
var bodyText = reader.ReadToEnd();
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()) , CultureInfo.CurrentCulture);
}
}
and in your Application_Start
:
ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());
and if you want to stick with the default factory which uses the JavaScriptSerializer class you could adjust the maxJsonLength
property in your web.config:
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="2147483644"/>
</webServices>
</scripting>
</system.web.extensions>
Solution 2
For me, the maxJsonLength was not being exceeded. I had to add the following:
<appSettings>
<add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>
It worked a treat after that.
Solution 3
The above suggestions didn't work for me until I tried this:
// Global.asax.cs
protected void Application_Start() {
MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}
Comments
-
Keith Nicholas about 4 years
I'm getting an exception that the JSON request was too large to be deserialized.
It's coming from the JsonValueProviderFactory....
The MVC App currently has a custom model binder using Json.Net which has no problem deserializing the json data. However I'm assuming the default JSON value provider is tripping up? or has some weird limit built into it?
It may be be to do with the latest release of MVC4 as when using the previous build of MVC4 there were no problems with large amounts of JSON.
So, is there a way to change the setting s of the actual json value binder?
I get the impression it's some custom thing that turns it into a dictionary....I can't find any source code related to it or if there are any settings I can change?
Or is there an alternative ValueBinder I could use?
or any other options?
Server Error in '/' Application. The JSON request was too large to be deserialized. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: The JSON request was too large to be deserialized. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [InvalidOperationException: The JSON request was too large to be deserialized.] System.Web.Mvc.EntryLimitedDictionary.Add(String key, Object value) +464621 System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +413 System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +373 System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) +164 System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext) +116 System.Web.Mvc.<>c__DisplayClassc.<GetValueProvider>b__7(ValueProviderFactory factory) +34 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +151 System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +177
-
Keith Nicholas about 12 yearsand sadly you only get a tiny amount of reputation for this, where someone who answers how to split a stirng in C# will get bucket loads :) Thanks! was really useful..... interestingly enough just making the maxJsonLength bigger didn't work. I got a feeling there might be a fault in the default Json Value binder
-
dave heywood almost 12 yearsyou hero..... I've been spending ages getting nowhere writing new handlers for the serialisation, when it was this all along..... thanks...
-
Eric Wild about 11 yearsMe too, this was the issue.
-
casaout over 10 yearsYou sir just saved my day! Thanks a lot!
-
Joseph Lennox almost 10 yearsAs my custom model binder is handling everything, I only called the remove. I don't want to be double deserializing everything!
-
Mihai Bratulescu about 9 yearsWhat if I send a
string
and deserialize it with JSON.NET? Will I ever get an error if the string is too big? -
Shaul Behr over 8 yearsBless your cotton socks, sir!
-
BJury about 8 yearsFor whatever reason the web.config change wasn't working, so I used your example and modified the actual JsonValueProviderFactory instead as I'm using MVC4 rather than 3.
-
kalai about 5 yearsMiniProfiler does not contains a definition for setting