Forum Discussion

mke4kor's avatar
mke4kor
Copper Contributor
Aug 23, 2024

Increase the file upload size to 50mb in the app service

Using .Net core 8.0 and App service Deployment , need to increase the file upload size limit to 50 mb
implemented following things in the local and its working as expected but when it is deployed to app services its not working

Program.cs file 
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel((context, options) =>
{
// Handle requests up to 50 MB
options.Limits.MaxRequestBodySize = 52428800;
})
.UseStartup<Startup>();
})
.UseSerilog()
;

 

startup file:
app.Use(async (context, next) =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 104857600; // unlimited I guess
await next.Invoke();
});

---

services.Configure<FormOptions>(o => // currently all set to max, configure it to your needs!
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = 104857600;
o.MultipartBoundaryLengthLimit = int.MaxValue;
o.MultipartHeadersCountLimit = int.MaxValue;
o.MultipartHeadersLengthLimit = int.MaxValue;
o.ValueCountLimit = int.MaxValue;

});
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 104857600; // Remove any limit
});

services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = 104857600; // Remove limits for Kestrel
});

-----

 

in app settings file 
"Kestrel": {
"Limits": {
"MaxRequestBodySize": 52428800
}
},

 

---

In the project configuration we are not using web.config file , we need alternate solution , if we use the web.config settings 
<system.web>
<httpRuntime targetFramework="4.8" maxRequestLength="1048576" executionTimeout="120" />
</system.web>

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

 

With this configuration it is working as expected in the app service but we are not web.config file as it is automatically generated 

 

3 Replies

  • mke4kor 

     

    Please ensure that the configurations are correctly applied and not overridden by other settings between Program.cs and Startup.cs

  • sdtslmn's avatar
    sdtslmn
    Brass Contributor
    if your application is behind an Azure Application Gateway or Front Door, ensure those services are also configured to allow larger file uploads.
    • mke4kor's avatar
      mke4kor
      Copper Contributor

      sdtslmn Application gateway is disabled and front door is configuraton is set a per the plan, the thing here is if i use IHttpPosetedFile instead of IFormFile the same app service works.

Resources