JsonValueProviderFactory抛出"要求太大" [英] JsonValueProviderFactory throws "request too large"

查看:349
本文介绍了JsonValueProviderFactory抛出"要求太大"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个异常的JSON请求是太大而不能反序列化。

这是一个从JsonValueProviderFactory ....

未来

在MVC应用程序目前使用Json.Net有没有问题反序列化JSON数据的自定义模型粘合剂。不过我假设默认值JSON提供者绊倒了?或者有一些怪限制内置到它?

这可能是与MVC4最新发布使用MVC4的previous编译时有与大量JSON没有问题,因为这样做。

那么,有没有办法改变实际的JSON值粘结剂的设置S'

按<一去href=\"http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx\">http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

我得到的IM pression它的一些自定义的东西,把它变成一本字典....我找不到任何来源$ C ​​$ C与之相关的或者有任何设置我可以改变?

或者是有一个替代ValueBinder我可以使用?

或任何其他的选择吗?

 在'/'应用程序的服务器错误。
该JSON请求是太大而不能反序列化。
说明:执行当前Web请求的执行过程中发生未处理的异常。请查看有关错误的详细信息的堆栈跟踪以及它起源于code。异常详细信息:System.InvalidOperationException:该JSON请求是太大而不能反序列化。源错误:当前Web请求的执行过程中生成了未处理的异常。有关异常的来源和位置信息可以使用异常堆栈跟踪下面确定。堆栈跟踪:[出现InvalidOperationException:本JSON请求是太大而不能反序列化。]
   System.Web.Mvc.EntryLimitedDictionary.Add(字符串键,对象的值)464621
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary备份存储,字符串preFIX,对象的值)413
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary备份存储,字符串preFIX,对象的值)+164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary备份存储,字符串preFIX,对象的值)+164
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary备份存储,字符串preFIX,对象的值)+373
   System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary备份存储,字符串preFIX,对象的值)+164
   System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)+116
   System.Web.Mvc&LT;&GT; c__DisplayClassc&LT; GetValueProvider&GT; b__7(ValueProviderFactory厂)+34       System.Linq.WhereSelectEnumerableIterator`2.MoveNext()+151
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext()177


解决方案

如果您使用JSON.NET的序列化/反序列化,你可以用自定义的替代默认JsonValueProviderFactory如图<一个href=\"http://www.dalsoft.co.uk/blog/index.php/2012/01/10/asp-net-mvc-3-improved-jsonvalueproviderfactory-using-json-net/\">this博客文章:

 公共密封类JsonDotNetValueProviderFactory:ValueProviderFactory
{
   公众覆盖IValueProvider GetValueProvider(ControllerContext controllerContext)
   {
        如果(controllerContext == NULL)
            抛出新的ArgumentNullException(controllerContext);        如果(!controllerContext.HttpContext.Request.ContentType.StartsWith(应用/ JSON,StringComparison.OrdinalIgnoreCase))
            返回null;        VAR读者=新的StreamReader(controllerContext.HttpContext.Request.InputStream);
        变种bodyText的= reader.ReadToEnd();        返回String.IsNullOrEmpty(bodyText的)?空:新DictionaryValueProvider&LT;对象&gt;(JsonConvert.DeserializeObject&LT; ExpandoObject&GT;(bodyText的,新ExpandoObjectConverter()),CultureInfo.CurrentCulture);
    }
}

和在的Application_Start

<$p$p><$c$c>ValueProviderFactories.Factories.Remove(ValueProviderFactories.Factories.OfType<JsonValueProviderFactory>().FirstOrDefault());
ValueProviderFactories.Factories.Add(新JsonDotNetValueProviderFactory());

和使用<一个,如果你想坚持使用默认出厂href=\"http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx\">JavaScriptSerializer类,你可以调整 maxJsonLength 财产在你的web.config:

 &LT; System.Web.Extensions程序&GT;
    &LT;脚本&GT;
        &LT;&Web服务GT;
            &LT; jsonSerialization maxJsonLength =2147483644/&GT;
        &LT; / WebServices的&GT;
    &LT; /脚本&GT;
&LT; /system.web.extensions>

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?

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

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

解决方案

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>

这篇关于JsonValueProviderFactory抛出&QUOT;要求太大&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆