Why doesn't my action method time out?

前端 未结 2 719
囚心锁ツ
囚心锁ツ 2020-12-16 13:45

I have the following controllers:

[TimeoutFilter]
public abstract class BaseController: Controller
{
}

public class IntegrationTestController : BaseControll         


        
2条回答
  •  情话喂你
    2020-12-16 14:03

    Are you using a debug build?

    From the ScriptTimeout documentation:

    http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.scripttimeout(v=vs.110).aspx

    If you set the debug attribute of the compilation element to true in the Web.config file, the value of ScriptTimeout will be ignored.

    Also, since this value is the same as that set on the httpRuntime element, I really don't understand the point of this, since you can just configure that setting in in your web.config instead.

    Edit:

    Dangerous has done a good job of finding out the details, and indeed, ScriptTimeout is unsupported in asynchronous pipelines (which MVC has been since at least MVC4 I think, as well as WebApi.. even if not using async methods)

    The "workaround" as suggested by this connect report:

    https://connect.microsoft.com/VisualStudio/feedback/details/781171/asp-net-mvc-executiontimeout-does-not-work

    Use the [AsyncTimeout] attribute, and take a cancelation token as a parameter, then call CanclationToken.ThrowIfCancelationRequested periodically, or use the cancellation token in an async method.

    Here's an example:

    [AsyncTimeout(5000)]
    public async Task Index(CancellationToken ct)
    {
        await Task.Delay(10 * 1000, ct);
        return Content("This should never get returned, mwahahaaa!");
    }
    

    This throws an OperationCanceled exception with a YSOD after 5 seconds. The bonus for this is that it works even in Debug mode ;)

提交回复
热议问题