57

I make use of ajax call in my ASP.NET Core MVC view pages

MyView.cshtml

          $.ajax({
                processData: false,
                contentType: false,
                data: new FormData(this),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                cache: false,
                success: function (data) {
                        $('#mydiv).html(data);
                        $('#bootstrapModal).modal('show');

Controller Post method"

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> MyLongRunningMethod(MyViewModel viewModel)
    {
        await MyProcess.Longprocess1();
        await MyProcess.Longprocess2();
        await MyProcess.Longprocess3();
        await MyProcess.Longprocess4();
        return PartialView("_MyPartialPage");
    }

This works but only has an issue for a long running process. I get the following error during debug mode when the process takes longer than around 2 minutes

enter image description here

I understand this is to do with expiration timeout

in previous versions of ASP.NET MVC you can apparently increase the timeout in your asp.net controller action.

HttpContext.Current.Server.ScriptTimeout = 90000;

However this doesn't exist in ASP.NET Core

I want to increase the timeout for debugging and deployment for a particular asp.net controller.

For production I can set it globally in the web.config by adding requestTimeout to the existing httpPlatform tag. e.g. for 10 minutes

<httpPlatform requestTimeout="00:10:00" ....

A similar question was asked but the answer giving using an CancellationToken but reading it, it doesn't seem it can help me with the timeouts.

  1. How do I set the timeouts in Visual Studio 2015 debug mode like I can do when I deploy it?
  2. IS there a per controller timeout setting like there was in ASP.NET 4?
1

7 Answers 7

63

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);
});
Sign up to request clarification or add additional context in comments.

6 Comments

This would be the best answer as by default web.config is not in the project.
This did not solve my issue, I still get 502 error, maybe because web.config is present in the project
Do you have 2.0 project?
when you publish the application with "dotnet publish" it generate the web.config so you can modify it
one important thing to note here is that will only work in Out-of-process hosting model as The requestTimeout attribute doesn't apply to in-process hosting : learn.microsoft.com/en-us/aspnet/core/host-and-deploy/…
|
54

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

Example for ASP.NET Core
<?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>

7 Comments

Using .net core 1.1 now and this seems to be working!
@firste did you find any solution for debug mode??
If you are running in Visual Studio, try to run in Console Mode instead of IIS. You should see an option that is the same name as your solution name in the dropdown that shows IIS, and this option does not seem to have the timeout limit.
Works with 2.0 too
Note, I don't think this works with .net core 3.1+ if you are using in-process hosting. requestTimeout is apparently not supported for this hosting model.
|
24

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);
                    }
                )

2 Comments

'KestrelServerOptions' does not contain a definition for 'Limits'. This is not an ASP.NET Core 1.0 solution.
Appears to exist in 2.2
21

The accepted answer has changed since .Net Core 3.1. In NET Core 3.1 use the following implementation to increase KeepAliveTimeout:

Program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.ConfigureKestrel(options =>
                {
                    options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
                });
            });

Web.Config (You need to add the config file to your project if it doesn't exist.)

Increase requestTimeout as required on the web.config:

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

3 Comments

thx for this - just confirming: is this an AND or an OR ie, do both or one or the other?
@toy I had to do both to get it working properly.
Web.config requestTimeout still won't do anything at all if you are hosting inprocess.
9

for me this worked:

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

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); });

Comments

4

For .net core 6.0

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(t =>
{
   t.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
});

Comments

0

In Visual Studio not appear file web.config in Solution Explore. I fixed adding the file in Solution and changed the property "Copy to output Directory".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.