Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method

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?
like image 527
devfric Avatar asked May 27 '16 03:05

devfric


People also ask

What is HTTPPost in ASP NET MVC?

HttpGet and HttpPost are both the methods of posting client data or form data to the server. HTTP is a HyperText Transfer Protocol that is designed to send and receive the data between client and server using web pages.

What is difference between HTTPPost and HTTPGet in MVC?

HTTPGet method creates a query string of the name-value pair whereas HTTPPost method passes the name and value pairs in the body of the HTTP request. 3. HTTPGet request has limited length and mostly it is limited to 255 characters long whereas HTTPPost request has no maximum limit.


2 Answers

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> 
like image 62
devfric Avatar answered Oct 12 '22 21:10

devfric


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(); } 
like image 44
Иванцов Дмитрий Avatar answered Oct 12 '22 19:10

Иванцов Дмитрий