Timeout a Web Api request?

后端 未结 4 652
轻奢々
轻奢々 2020-12-30 03:37

Like MVC WebApi runs on the asynchronous ASP.NET pipeline, meaning execution timeout is unsupported.

In MVC I use the [AsyncTimeout] filter, WebApi does

4条回答
  •  不知归路
    2020-12-30 04:06

    For each endpoint where you want a timeout, pipe a CancellationToken through, e.g.:

    [HttpGet]
    public Task GetAsync()
    {
        var tokenSource = new CancellationTokenSource(_timeoutInSec * 1000);
        return GetResponseAsync(tokenSource.Token);
    }
    

提交回复
热议问题