granadaCoder
granadaCoder

Reputation: 27904

Is is possible to set the "aspNetCore requestTimeout" value.........via "code" with Kestrel?

Is is possible to set the "aspNetCore requestTimeout" value (see xml below).........via "code" with Kestrel?

I cannot find something on the KestrelServerLimits object.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.server.kestrel.core.kestrelserverlimits?view=aspnetcore-3.1

Below is the xml code..........but hoping to do this in CODE, not via (published) xml.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore requestTimeout="00:20:00"  processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

I tried the below (" serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(5); ")......but this does not control the server timeout from my postman tests.

namespace MyStuff
{
    using System;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();

                    webBuilder.ConfigureKestrel(serverOptions =>
                    {
                        serverOptions.Limits.KeepAliveTimeout = TimeSpan.FromSeconds(5);
                    });

                });
    }
}

I've been reading from this microsoft article:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel?view=aspnetcore-3.1#kestrel-options

Kestrel options The Kestrel web server has constraint configuration options that are especially useful in Internet-facing deployments.

Set constraints on the Limits property of the KestrelServerOptions class. The Limits property holds an instance of the KestrelServerLimits class.

Upvotes: 4

Views: 12019

Answers (1)

Dmitry
Dmitry

Reputation: 16825

No. This GitHub issue says:

There's no way to forcibly terminate .NET Tasks. This is why we can't stop run-away CPU processes ourselves.

requestTimeout in web.config is a timeout for IIS that will stop waiting response from your webapp and return 500 error to client, but your webserver (Kestrel) will continue to process request. And nobody can interrupt it (only restarting kestrel process will help), that's why there is no such configuration setting in kestrel.

Upvotes: 8

Related Questions