'How to achieve strictly file size limit in Abp

I'm trying to set strictly file size limit using filter, in the mean while, there is already a default global setting at Startup.cs

Startup.cs

int sizeLimit = 200000;

services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = sizeLimit;
});

services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = sizeLimit;
});

services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = sizeLimit);

TestApiController.cs or Service(Dynamic Controller)

[Route("[controller]")]
public class TestApiController : Controller
{
    [HttpPost]
    [RequestSizeLimit(100_000)]
    [RequestFormLimits(MultipartBodyLengthLimit = 100_000)]
    public bool Post(IFormFile file)
    {
        return true;
    }
}

I found warning log when i upload file

A request body size limit could not be applied. The IHttpRequestBodySizeFeature for the server is read-only.

I seem to need to set IHttpMaxRequestBodySizeFeature IsReadOnly to false, but it can't. There's not setter at IsReadOnly.

app.Run(async (context) =>
{
    var httpMaxRequestBodySizeFeature = context.Features.Get<IHttpMaxRequestBodySizeFeature>();
    httpMaxRequestBodySizeFeature.IsReadOnly = false;
});

Then I found it will affected by IsUpgraded, If IHttpUpgradeFeature.UpgradeAsync be called will set IsUpgraded to true. I wonder this method may have called at somewhere, but i can't figure it out.

bool IHttpMaxRequestBodySizeFeature.IsReadOnly => HasStartedConsumingRequestBody || IsUpgraded;

ref IHttpUpgradeFeature.UpgradeAsync()



Solution 1:[1]

need to use customized attribute https://github.com/abpframework/abp/issues/12336

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Billy Chung