Nancy maximum JSON length exception when Binding

£可爱£侵袭症+ 提交于 2019-12-12 18:23:31

问题


I have simple Nancy self hosting C# project which deserializes data in NancyModule like this:

Post["/build"] = (something) => { var data = this.Bind<Brick>(); }

I am getting "Maximum JSON input length has been exceeded." when Request.Body.Length is close to 2MB. I would like to send tens of megabytes of data in the future and 2MB is just too low. Can I remove this limit?


回答1:


You can override the MaxJsonLength and the MaxRecursions. Basically I did a custom bootstrapper something like this:

public class CustomBootStrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            Nancy.Json.JsonSettings.MaxJsonLength = int.MaxValue;
            Nancy.Json.JsonSettings.MaxRecursions = 100;
            Nancy.Json.JsonSettings.RetainCasing = true;
            base.ApplicationStartup(container, pipelines);
        }
    }



回答2:


For me Nancy didn't work all together even after applying below settings. As my data was really really huge.

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

I ended up using Json.net (add via nuGet package)

  JsonConvert.SerializeObject(data)


来源:https://stackoverflow.com/questions/45046242/nancy-maximum-json-length-exception-when-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!