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

查看:123
本文介绍了使用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.

为什么?

UPD:
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天全站免登陆