Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the request timeout for one controller action on IIS and IIS Express

I need to increase the request timeout for a specific controller action in my application programmatically in the controller.

like image 483
Stanislav Prusac Avatar asked Jan 06 '16 16:01

Stanislav Prusac


People also ask

How do I set request timeout in .NET core?

But according to the documentation you can just add web. config to your project and specify this (and other) setting value: Setting the RequestTimeout="00:20:00" on the aspNetCore tag and deploying the site will cause it not to timeout.


1 Answers

The best way I have found so far to handle a long running script (if your application simply can't avoid having one) is to firstly create an async handler:

public async Task<IActionResult> About()
    {
            var cts = new CancellationTokenSource();            
            cts.CancelAfter(180000);
            await Task.Delay(150000, cts.Token);
        return View();
    }

In the snippet above I have added a Cancellation token to say that my Async Method should cancel after 180 seconds (even though in this example it will never delay for longer than 150 seconds) I've added the timeout to ensure there is a cap on how long my script can run.

If you run this you will get a HTTP Error 502.3 since there is a timeout set on the httpPlatform of 00:02:00.

To extend this timeout go to the web.config file inside the wwwroot folder and add a requestTimeout="00:05:00" property to the httpPlatform element. For Example:

<httpPlatform requestTimeout="00:05:00" processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>

The long running script should now work.

This does, however, mean that any request will take 5 minutes to timeout so you will have to ensure that you use the CancellationToken trick to limit any Async requests you have in your application. In previous versions of MVC we had the AsyncTimeOut attribute that you could add to a controller, but it appears that this has been cut in MVC6 See GitHub aspnet/mvc Issue #2044

like image 67
Martin Beeby Avatar answered Oct 05 '22 02:10

Martin Beeby