skip to Main Content

I have an ASP.NET Core 6 Web API running on a container on Kubernetes.
The Container uses the mcr.microsoft.com/dotnet/sdk:6.0 image to build the application and runs on mcr.microsoft.com/dotnet/aspnet:6.0 image.

Here’s the Dockerfile:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /App

# Copy everything
COPY . ./

# Restore as distinct layers
RUN dotnet restore

# Build and publish a release
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /App
COPY --from=build-env /App/out .
ENTRYPOINT ["dotnet", "MyProjectName.WebAPI.dll"]

Problem is: A client makes really big requests to a specific endpoint on my API, and the API is returning (513) Request Entity Too Large error.

What I already tried

  • Adding [DisableRequestSizeLimit] annotation to the controller method being requested. (Didn’t change anything. Error 513 still happens.)

  • Adding a new web.config file (which didn’t exist before) with the following contents:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
      <security>
          <requestFiltering>
              <requestLimits maxAllowedContentLength="2000000000" />
          </requestFiltering>
      </security>
  </system.webServer>
</configuration>

Looks like this file is only useful for IIS. But I am not sure if the container runs IIS… Probably not.

Actually I am not even sure if the container runs Nginx, Kestrel or something else.

It runs on ISS Express on my local environment. It only runs on a container on the production environment.

Any ideas on what else can I do?

2

Answers


  1. If your ASP.NET Core 6 Web API is running in a container on Kubernetes, it is most likely using Kestrel as the web server, not IIS or Nginx. The base image mcr.microsoft.com/dotnet/aspnet:6.0 typically includes Kestrel as the default web server.

    To address the "413 Request Entity Too Large" error, you need to configure Kestrel to allow larger request entity sizes. You can do this by adding Kestrel configuration settings to your application.

    Create a appsettings.json or appsettings.Production.json (if you have different configurations for production) file in your ASP.NET Core project (if it doesn’t exist already).

    Add the following Kestrel configuration to the appsettings.json file:

    {
      "Kestrel": {
        "Limits": {
          "MaxRequestBodySize": 2000000000
        }
      }
    }
    

    This configuration will set the maximum request body size to 2 GB, which should be sufficient for handling large requests.

    Make sure your Program.cs file configures the app to read the configuration from the appsettings.json file. In the CreateHostBuilder method, add the following line:

    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    })
    

    Your CreateHostBuilder method may look like this after the modification:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                });
                webBuilder.UseStartup<Startup>();
            });
    

    Rebuild your container image with the updated application files and configuration.

    Deploy the new container to Kubernetes.

    With this configuration, Kestrel should now allow larger requests, and you should no longer encounter the "413 Request Entity Too Large" error. Remember to adjust the MaxRequestBodySize value as needed based on your application’s requirements.

    Login or Signup to reply.
  2. I suggest you could also try to modify the MaxRequestBodySize inside the program.cs like below:

    builder.WebHost.ConfigureKestrel(serverOptions =>
    {
        serverOptions.Limits.MaxRequestBodySize = 100_000_000;
    });
    

    More details, you could refer to this article.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search