Upload large files with .net core ⏏️

Agustinafassina
2 min readApr 24, 2022

--

If you are working with .net core and it refuses to upload large files, this article is for you.
I share some recommendations to keep in mind when working with large files and how to configure our service based on our needs, with a service without limits.

Requirements before starting:
· Download: https://dotnet.microsoft.com/download/dotnet/3.1
· Optional: https://git-scm.com/downloads
· My repository: https://github.com/agustinafassina/UploadLargeFiles

For the service to support heavy file uploads, I have three things in mind:

1- The class Program.cs
2- The class Startup.cs
3- The most important: where you host the services. In this case of using aws ser, it is very important that the instance where you host the services can process the file.

1- The class Program.cs and the method IHostBuilder

Gets or sets the maximum allowed size of any request body in bytes. When set to null, the maximum request body size is unlimited.

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
});
});

2- The class Startup.cs and the method ConfigureServices.

services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = int.MaxValue);
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MultipartBoundaryLengthLimit = int.MaxValue;
x.MultipartHeadersCountLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
});
options.Limits.MaxRequestBodySize = null;
});

3- The worst thing about working with large files is that it can explode everything with one click, if it explodes the instance you are working on. I had the possibility of being able to experience giant loads in production, before reaching that environment luckily we went through others haha! In the first test I explode everything, in a t2.medium instance with about 10 services. We pass to a t2.xlarge instance and process without problems.

t2.medium → t2.xlarge

Related documentations:

Well guys, it’s always a pleasure to write small contributions to this community, I recently started taking my first steps in DevOps, that’s why I’m leaving you a documentation that I was reading when I had to change instances from AWS.
My linkedin is https://www.linkedin.com/in/agustina-fassina-458247163.

See you on the next article!!

--

--