如何为IIS和IIS Express上的一个控制器操作设置请求超时 [英] How to set the request timeout for one controller action on IIS and IIS Express

查看:92
本文介绍了如何为IIS和IIS Express上的一个控制器操作设置请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控制器中以编程方式增加对应用程序中特定控制器动作的请求超时.

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

推荐答案

到目前为止,我发现处理长时间运行的脚本(如果您的应用程序无法避免拥有一个脚本)的最佳方法是首先创建一个异步处理程序:

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

在上面的代码段中,我添加了一个Cancellation令牌,表示我的异步方法应在180秒后取消(即使在此示例中,它的延迟时间永远不会超过150秒),我添加了超时以确保存在我的脚本可以运行多长时间的上限.

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.

如果运行此命令,则会收到HTTP错误502.3,因为在httpPlatform上设置的超时时间为00:02:00.

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.

要延长此超时时间,请转到wwwroot文件夹内的web.config文件,并将requestTimeout ="00:05:00"属性添加到httpPlatform元素.例如:

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.

但是,这确实意味着任何请求都将花费5分钟超时,因此您必须确保使用CancellationToken技巧来限制应用程序中的所有异步请求.在早期版本的MVC中,我们具有可以添加到控制器的AsyncTimeOut属性,但是看来这已在MVC6中被剪切请参阅GitHub aspnet/mvc问题#2044

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

这篇关于如何为IIS和IIS Express上的一个控制器操作设置请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆