Occasional exception with JSON JavaScriptSerializer "The length of the string exceeds [maxJsonLength]"


#1

Just started using MiniProfiler in a .NET 4.0 MVC3 app with EF5, and generally love it. It works well, and is valuable.

However, occasionally (seems like it’s most often right after starting a fresh build) the request will take a long time, then an InvalidOperationException will be thrown. After continuing past the exception everything seems okay, and re-requesting the same page works quickly and without error.

MiniProfiler was added to the project through NuGet, which reports version 3.1.1.140 for MiniProfiler

Exception message: “Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.”

Inner Exception: null

Stack Trace:
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)
at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)
at StackExchange.Profiling.MiniProfiler.ToJson(MiniProfiler profiler) in c:\TeamCity\buildAgent\work\1de24adb938b932d\StackExchange.Profiling\MiniProfiler.cs:line 349
at StackExchange.Profiling.MiniProfilerHandler.ResultsJson(HttpContext context, MiniProfiler profiler) in c:\TeamCity\buildAgent\work\1de24adb938b932d\StackExchange.Profiling\MiniProfilerHandler.cs:line 349
at StackExchange.Profiling.MiniProfilerHandler.GetSingleProfilerResult(HttpContext context) in c:\TeamCity\buildAgent\work\1de24adb938b932d\StackExchange.Profiling\MiniProfilerHandler.cs:line 343
at StackExchange.Profiling.MiniProfilerHandler.ProcessRequest(HttpContext context) in c:\TeamCity\buildAgent\work\1de24adb938b932d\StackExchange.Profiling\MiniProfilerHandler.cs:line 93
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)


#2

This can happen if you are profiling so many sql events (or any other type of event) in one request that when the value is sent to the browser, the JSON is just too long. I have had this happen to me when I was profiling some really long background data syncing actions that had literally hundreds of sql events being profiled.

Solution for me was to identify where this was and to do MiniProfiler.Stop() to not profile that specific action.

If you really want to profile it and are ok with the super-long JSON, then you can play around with the maxJsonLength value in your web.config to find something that works for you.


#3

According to this post
https://code.google.com/p/mvc-mini-profiler/issues/detail?id=104
the Web.config setting is ignored. Instead you need to set the MiniProfiler.Settings.MaxJsonResponseSize property.