Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net web service maximum json input length has been exceeded

I got a webserver in .net working with Nancyfx. I have a route that has to upload an image, this image is sent from a client in json byte64 encoded, along with other attributes. When I try to bind the incoming json with my model, I got the next exception: "Maximum JSON input length has been exceeded."

Something like this:

Post["/Upload", true] = async(_, ctx) =>
{
     UploadModel model = null;

     model = this.Bind<UploadModel >();

   .
   .
   .
}

I've read that changing the value of "maxJsonLength" in my web.config handles this issue, but when i set a higher value to it, there's no effect:

<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="50000000"/>
  </webServices>
</scripting>

Along with the maxRequestLength:

<httpRuntime targetFramework="4.5" maxRequestLength="1000000"/>

For some smaller pictures (5KB, 50KB) there's no problem in binding, but when I send pictures with sizes of 144KB and up, it gives me the error in concern.

Any thoughts? If I missed some relevant information, just ask me

like image 210
monkeyBug Avatar asked Jun 23 '14 19:06

monkeyBug


1 Answers

Never mind guys, I just found the answer:

In the nancy documentation it says "if you encounter the Nancy.Json.JsonSettings.MaxJsonLength Exceeded error because your payload is too high, change that limit in your Bootsrapper ..."

So i did it:

public class Bootstrapper : DefaultNancyBootstrapper
{
        protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            Nancy.Json.JsonSettings.MaxJsonLength = int.MaxValue;
        }
}

Now, no more MaxJsonLength errors, hope this helps to somebody in the future!

like image 181
monkeyBug Avatar answered Sep 21 '22 09:09

monkeyBug