带有 json 的 asp web.api 的最大 http 请求大小 [英] Maximum http request size for asp web.api with json

查看:22
本文介绍了带有 json 的 asp web.api 的最大 http 请求大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 web api 项目.

I have web api project.

我需要将带有文件的 json 数据作为编码的 base64 字符串(最多 200 mb)发布.

I need to post there json data with file as encoded base64 string (up to 200 mb).

如果我发送的数据最大约为 10 mb,那么下一个方法通常会正确填充模型 ImportMultipleFileModel.

If i send data up to about 10 mb, then next method normally get properly filled model ImportMultipleFileModel.

[HttpPost]
    public async Task<HttpResponseMessage> ImportMultipleFiles(ImportMultipleFileModel importMultipleFileModel)
    { 
        var response = ImportFiles(importFileModel);
        return response;
    }

如果我发送更多,则模型为空.

If i send more, then model is null.

为什么?

所以我将方法签名更改为:

So i change method signature to:

    [HttpPost]
        public async Task<HttpResponseMessage> ImportMultipleFiles()
        {
            ImportMultipleFileModel importMultipleFileModel = null;
            var requestData = await Request.Content.ReadAsStringAsync();
            try
            {
                JsonConvert.
                importMultipleFileModel = JsonConvert.DeserializeObject<ImportMultipleFileModel>(requestData);
            }catch(Exception e)
            { }
}

对于编码的 30 mb 文件,我通常将 requestData 作为 json 字符串.对于 60 mb,我得到空字符串.为什么?

And for encoded 30 mb file i normally get requestData as json string. For 60 mb i get empty string. Why?

接下来我将方法更改为

    [HttpPost]
        public async Task<HttpResponseMessage> ImportMultipleFiles()
        {
            ImportMultipleFileModel importMultipleFileModel = null;
            var requestData = Request.Content.ReadAsStringAsync().Result;
            try
            {
                importMultipleFileModel = JsonConvert.DeserializeObject<ImportMultipleFileModel>(requestData);
            }catch(Exception e)
            { }
}

由于 OutOfMemoryException,反序列化失败.

And deserialization failed because of OutOfMemoryException.

为什么?

更新:maxRequestLength, maxAllowedContentLength 设置为 2147483647

UPD: maxRequestLength, maxAllowedContentLength set to 2147483647

推荐答案

尝试设置 maxRequestLength.

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

maxAllowedContentLength (我总是搞不清楚哪个是哪个).

Or maxAllowedContentLength (I always get confused which one's which).

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="52428800" />
  </requestFiltering>
</security>

另外,我会重新考虑以这种方式发布数据.阅读 这篇文章来自 MSDN,主要是对于 WCF,但我认为内容大部分是有效的.

Also, I would reconsider posting data this way. Read this article form MSDN, it's mainly for WCF, but I think the content is mostly valid.

处理大型负载的策略是流式传输.

The strategy to deal with large payloads is streaming.

最后一个例子的旁注;当您可以使用 await 时,您不应该(或者可能很少)使用 .Result.Stephen Cleary 写了一个很好的答案 此处.

Side note for your last example; you should not (or perhaps rarely) use .Result when you can use await. Stephen Cleary wrote a good answer on that here.

这篇关于带有 json 的 asp web.api 的最大 http 请求大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆