Jsonvaluproviderfactory lance une demande trop importante"

j'obtiens une exception que la demande JSON était trop grande pour être deserialized.

ça vient du Jsonvaluproviderfactory....

L'application MVC a actuellement un liant de modèle personnalisé en utilisant Json.Net ce qui n'a aucun problème pour désérialiser les données json. Cependant, je suppose que le fournisseur de valeur JSON par défaut est en train de trébucher? ou a peu bizarre limite construit en elle?

il peut être à voir avec la dernière version de MVC4 comme lors de l'utilisation de la version précédente de MVC4, il n'y avait aucun problème avec de grandes quantités de JSON.

alors, y a-t-il un moyen de changer les paramètres du liant JSON actuel?

en passant par http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

j'ai l'impression que c'est quelques chose qui tourne dans un dictionnaire....Je ne trouve pas le code source lié à ça ou si il y a des paramètres que je peux changer?

ou y a-t-il un autre expert de valeur que je pourrais utiliser?

ou d'autres 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
28
demandé sur Keith Nicholas 2012-03-01 06:19:17

3 réponses

si vous utilisez JSON.NET pour serialization/ deserialization, vous pouvez substituer le JsonValueProviderFactory par défaut avec un custom one Comme Montré dans ce billet de blog :

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);
    }
}

et dans votre Application_Start :

ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(new JsonDotNetValueProviderFactory());

et si vous voulez vous en tenir à l'usine par défaut qui utilise la classe JavaScriptSerializer vous pouvez ajuster la maxJsonLength propriété dans votre web.config:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="2147483644"/>
        </webServices>
    </scripting>
</system.web.extensions>
65
répondu Darin Dimitrov 2012-03-01 07:15:44

pour moi, le maxJsonLength n'était pas dépassé. J'ai dû ajouter ce qui suit:

<appSettings>
  <add key="aspnet:MaxJsonDeserializerMembers" value="150000" />
</appSettings>

ça a marché après ça.

62
répondu Doug Wilson 2012-03-14 15:52:17

les suggestions ci-dessus n'ont pas fonctionné pour moi jusqu'à ce que j'ai essayé ceci:

// Global.asax.cs    
protected void Application_Start() {
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue;
}
3
répondu Sam Rueby 2012-12-12 23:03:38