2012-03-01 30 views
28

JSONリクエストが大きすぎてデシリアライズできないという例外が発生しました。 MVCアプリケーションは現在、JSONデータをデシリアライズ問題がないJson.Netを使用してカスタムモデルバインダーを持ってJsonValueProviderFactoryが「要求が大きすぎます」を返します

それはJsonValueProviderFactoryから来ています....。しかし、私はデフォルトのJSON値プロバイダがトリプアップしていると仮定していますか?それに奇妙な限界がありますか?

MVC4の以前のビルドを使用している場合、大量のJSONに問題はなかったので、MVC4の最新リリースと関係があるかもしれません。

実際のjson値バインダーの設定を変更する方法はありますか?

私はそれが辞書に変換しますいくつかのカスタムのことだ印象を得る http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx

で行く

....私はそれまたは私は変更することができます任意の設定がある場合は関連するすべてのソースコードを見つけることができませんか?

代わりに使用できる別のValueBinderがありますか?

または他のオプションはありますか?

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 

答えて

64

あなたがシリアライズ/デシリアライゼーションのためJSON.NETを使用する場合はthis blog postに示すように、カスタムのいずれかで、デフォルトJsonValueProviderFactoryに置き換えることができます:

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

とあなたのApplication_Startで:

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

の場合、JavaScriptSerializerクラスを使用するデフォルトのファクトリを使用する場合は、あなたのweb.configのプロパティ:

<system.web.extensions> 
    <scripting> 
     <webServices> 
      <jsonSerialization maxJsonLength="2147483644"/> 
     </webServices> 
    </scripting> 
</system.web.extensions> 
+4

をし、悲しげにあなただけのC#でstirngを分割する方法を答える誰かが取得されます。このための評判の小さな量を取得バケツロード:)ありがとう!興味深いことに、maxJsonLengthを大きくするだけではうまくいかなかった。私はデフォルトのJson Valueバインダーに欠陥があるかもしれないという気持ちを持っています –

+0

私のカスタムモデルのバインダーはすべてを処理しているので、私はremoveを呼び出しました。私はすべてをデシリアライズすることはしたくない! –

+0

JSON.NETで 'string'をデシリアライズするとどうなりますか?文字列が大きすぎるとエラーが発生しますか?私が取得年代を過ごしてきた..... web.configファイルの変更が働いていなかったので、私はあなたの例を使用し、私はむしろ3. –

61

私にとっては、maxJsonLengthを超えていませんでした。私は以下を追加しなければならなかった:

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

それは後に治療をした。

+3

よりも、あなたのヒーローをMVC4を使用しているとして、代わりに実際のJsonValueProviderFactoryを修正何らかの理由で –

+2

私も、これが問題でした。 –

+1

偉大な答え!それは私を助けた!まことにありがとうございます! – Bronek

3

私はこの試みたまで、上記の提案は、私のために動作しませんでした:

// Global.asax.cs  
protected void Application_Start() { 
    MiniProfiler.Settings.MaxJsonResponseSize = int.MaxValue; 
} 
関連する問題