Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method
Setting the RequestTimeout="00:20:00"
on the aspNetCore
tag and deploying the site will cause it not to timeout.
Issue now only occurs when running from Visual Studio debug mode but not in release mode.
Take note: In ASP.NET RTM the httpPlatform
tag has been replaced with aspNetCore
in the web.config
<?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>
In my case we still had the 502 timeout error at 2 minutes even after updating the requestTimeout
parameter. It turned out that kestrel has a 2 minute keep alive timeout which needed to be overridden:
var host = new WebHostBuilder()
.UseKestrel(o =>
{
o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
}
)
But in .Net Core 2.0 there is no web.config
file in project. It generate automatically.
I solved the problem by adding .UseKestrel(...)
to the BuildWebHost
function in Program.cs
file as follows:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); })
.Build();
}
... Update for .net Core 6 ...
in Program.cs
file after
var builder = WebApplication.CreateBuilder(args);
add ConfigureKestrel
for WebHost
like this
builder.WebHost.ConfigureKestrel(c =>
{
c.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
});