-1

I wonder if someone could help or point me in the right direction as a Sitecore novice.

I have a Sitecore MVC controller wired up to accept a JSON request. Within this request can be a Base64 encoded image. Any image over the 2mb mark will cause a 500 error and an entry in the Sitecore logs(log below). The method is never hit so the problem must be before it. I've tried adding numerous web.config entries to raise this limit to no avail and I believe it is a Sitecore restriction.

Can anyone help of shed any light this?

Many thanks

17964 09:32:43 ERROR Application error.
Exception: System.Web.HttpUnhandledException
Message: An unhandled exception occurred.
Source: Sitecore.Mvc
   at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.ShowErrorMessage(ExceptionContext exceptionContext, ExceptionArgs args)
   at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.Process(ExceptionArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Filters.PipelineBasedRequestFilter.OnException(ExceptionContext exceptionContext)
   at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at Sitecore.Mvc.Controllers.SitecoreActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.<>c__DisplayClass22.<BeginExecuteCore>b__1e()
   at System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at Sitecore.Mvc.Routing.RouteHttpHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.ArgumentException
Message: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Parameter name: input
Source: System.Web.Extensions
   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
   at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
   at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ControllerBase.get_ValueProvider()
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)


17964 09:32:43 ERROR Catching Exception in Global.asax_Application_Error
Exception: System.Web.HttpUnhandledException
Message: An unhandled exception occurred.
Source: Sitecore.Mvc
   at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.ShowErrorMessage(ExceptionContext exceptionContext, ExceptionArgs args)
   at Sitecore.Mvc.Pipelines.MvcEvents.Exception.ShowAspNetErrorMessage.Process(ExceptionArgs args)
   at (Object , Object[] )
   at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
   at Sitecore.Mvc.Pipelines.PipelineService.RunPipeline[TArgs](String pipelineName, TArgs args)
   at Sitecore.Mvc.Filters.PipelineBasedRequestFilter.OnException(ExceptionContext exceptionContext)
   at System.Web.Mvc.ControllerActionInvoker.InvokeExceptionFilters(ControllerContext controllerContext, IList`1 filters, Exception exception)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at Sitecore.Mvc.Controllers.SitecoreActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
   at System.Web.Mvc.Controller.<>c__DisplayClass22.<BeginExecuteCore>b__1e()
   at System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult, Action action)
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at Sitecore.Mvc.Routing.RouteHttpHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Nested Exception

Exception: System.ArgumentException
Message: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Parameter name: input
Source: System.Web.Extensions
   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
   at System.Web.Mvc.JsonValueProviderFactory.GetDeserializedObject(ControllerContext controllerContext)
   at System.Web.Mvc.JsonValueProviderFactory.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ValueProviderFactoryCollection.GetValueProvider(ControllerContext controllerContext)
   at System.Web.Mvc.ControllerBase.get_ValueProvider()
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
   at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)


24124 09:32:47 INFO  [Experience Analytics]: Reduce agent found zero segments to process
ManagedPoolThread #4 09:32:52 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #4 09:32:52 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #16 09:33:02 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #16 09:33:02 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #3 09:33:12 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #3 09:33:12 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
24124 09:33:17 INFO  [Experience Analytics]: Reduce agent found zero segments to process
ManagedPoolThread #9 09:33:22 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #9 09:33:22 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #13 09:33:33 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #13 09:33:33 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #1 09:33:43 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #1 09:33:43 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
24124 09:33:47 INFO  [Experience Analytics]: Reduce agent found zero segments to process
ManagedPoolThread #12 09:33:53 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #12 09:33:53 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #8 09:34:03 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #8 09:34:03 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
ManagedPoolThread #19 09:34:13 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #19 09:34:13 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
24124 09:34:17 INFO  [Experience Analytics]: Reduce agent found zero segments to process
ManagedPoolThread #5 09:34:23 INFO  Job started: Sitecore.ListManagement.Analytics.UnlockContactListsAgent
ManagedPoolThread #5 09:34:23 INFO  Job ended: Sitecore.ListManagement.Analytics.UnlockContactListsAgent (units processed: )
0

There is a 2Mb limit on JSon content in MVC. You could add following to the web.config:

<system.web.extensions>
    <scripting>
        <webServices>
            <jsonSerialization maxJsonLength="1000000000" />
        </webServices>
    </scripting>
</system.web.extensions>

Bear in mind that this limit is imposed by the webserver, not by your choice of protocol.

2
  • Thank you for the response, but I have already tried that entry to no avail. I wondered if the issue could be related to this post: sitecore.stackexchange.com/questions/3717/… May 9 '19 at 11:08
  • I see - sorry, I hadn't tried this in a Sitecore context yet - Adam's answer may be the solution you are looking for. May 9 '19 at 11:10
0

I haven't tested this exact scenario but I think Emanuele's answer might not work as I believe Sitecore ignores this setting as I've had this kind of issue before (see: Link Browser doesn't show all child items from Tree?).

I came across this Blog Post by Dirk Schäfauer which has an answer for this: https://seitenkern.com/2014/03/28/sitecore-7-mvc-json-serialization-and-maxjsonlength/

reposing my updated code below from my SSE answer linked above. This code is outdated for 8.1 (I'm not sure about 8.0 but it's likely to be pretty similar to the code below):

JsonSerializer:

namespace SitecoreContrib.Serialization
 {
      public class LargeJsonSerializer : ISerializer
    {
        public string SerializedDataMediaType
        {
            get { return "application/json"; }
        }

        public string Serialize(object value)
        {
            Assert.ArgumentNotNull(value, "value");
            var scriptSerializer = new JavaScriptSerializer { MaxJsonLength = 2097152 };
            var setting = Settings.GetSetting("JsonSerialization.MaxLength");
            int result;
            if (!string.IsNullOrEmpty(setting) && !scriptSerializer.MaxJsonLength.ToString(CultureInfo.InvariantCulture).Equals(setting, StringComparison.InvariantCultureIgnoreCase) && int.TryParse(setting, out result))
            {
                scriptSerializer.MaxJsonLength = result;
            }
            return scriptSerializer.Serialize(value);
        }
    }
  }

Pipeline:

namespace SitecoreContrib.Serialization.Pipelines.PreprocessRequest
{
      public class RewriteUrlForLargeJsonResponse : RewriteUrl
    {
        public override void Process(PreprocessRequestArgs arguments)
        {
            Assert.ArgumentNotNull((object)arguments, "arguments");
            try
            {
                string localPath = arguments.Context.Request.Url.LocalPath;
                if (!localPath.StartsWith("/-/item/"))
                    return;
                Sc.ItemWebApi.Context.Current = new Sc.ItemWebApi.Context
                {
                    Serializer = (ISerializer)new LargeJsonSerializer(),
                    Version = GetVersion(localPath),
                    ResponseOutputBuilder = new ResponseOutputBuilder()
                };
                Rewrite(arguments.Context);
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private static int GetVersion(string path)
        {
            Assert.ArgumentNotNull((object)path, "path");
            string str = path.TrimStart('/').Split('/')[2];
            Assert.IsTrue(str.StartsWith("v"), "Version token is wrong.");
            int result;
            Assert.IsTrue(int.TryParse(str.Replace("v", string.Empty), out result), "Version not recognized.");
            return result;
        }

        private static void Rewrite(HttpContext context)
        {
            Assert.ArgumentNotNull((object)context, "context");
            Uri url = context.Request.Url;
            string[] strArray1 = url.LocalPath.TrimStart('/').Split('/');
            int length = strArray1.Length - 3;
            string[] strArray2 = new string[length];
            Array.Copy((Array)strArray1, 3, (Array)strArray2, 0, length);
            string str1 = string.Format("/{0}", (object)string.Join("/", strArray2));
            string str2 = url.Query.TrimStart('?');
            WebUtil.RewriteUrl(new UrlString
            {
                Path = str1,
                Query = str2
            }.ToString());
        }
    }
 }

Config:

<sitecore>
    <pipelines>
      <preprocessRequest>
        <processor type="SitecoreContrib.Serialization.Pipelines.PreprocessRequest.RewriteUrlForLargeJsonResponse, SitecoreContrib.Serialization" patch:instead="*[@type='Sitecore.ItemWebApi.Pipelines.PreprocessRequest.RewriteUrl, Sitecore.ItemWebApi']" />
      </preprocessRequest>
    </pipelines>
    <settings>
      <!-- JsonSerialization.MaxLength
           Specifies the maximum length of JSON strings which can be serialized by the JsonSerializer.
           Value is specified in bytes. Default value: 2097152 (2 MB)
      -->
      <setting name="JsonSerialization.MaxLength" value="2147483647" />
    </settings>
  </sitecore>

If the suggested web.config setting doesn't fix your issue then try this instead.

2
  • Ah thank you, I did see this but we weren't sure if this was applicable to our problem. I'll give it a go and report back. Thank you. May 9 '19 at 11:14
  • No worries. I think it is likely the same issue given the problem is a limit on the Json response size and not specific to an item type as such May 9 '19 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.