【问题标题】:asp.net core: how to set the requesttimeout for a single action/route?asp.net core:如何为单个操作/路由设置请求超时?
【发布时间】:2018-03-03 11:41:45
【问题描述】:

在asp.net core中,如何为单个操作设置requesttimeout

我知道我可以设置 webconfig:

但这是全局性的,我希望仅对一项操作有更长的超时时间。

【问题讨论】:

    标签: asp.net asp.net-core asp.net-core-webapi


    【解决方案1】:

    您可以在控制器方法中自己实现超时:

    public async Task<IActionResult> DoNotHang()
    {
        var timeout = TimeSpan.FromMinutes(1);
        var operation = LongOperationAsync(); // no "await" here!
    
        if (await Task.WhenAny(operation, Task.Delay(timeout)) == operation)
        {
            var result = await operation; // obtain result (if needed)
            return Ok(result == null ? "Null" : "Not null");
        }
        else
        {
            return Ok("Timed out (but still working, not cancelled)");
        }
    }
    

    请参阅Asynchronously wait for Task<T> to complete with timeout 了解有关超时等待的更多详细信息,包括在完成时取消“其他”任务。

    【讨论】:

      猜你喜欢
      • 2010-10-09
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-29
      • 2011-04-19
      相关资源
      最近更新 更多