在asp.net core中,如何为单个动作route设置requesttout?requesttimeout
为一个动作?
但这是全局的,而且我只想在一个动作上有一个较长的超时。
你可以自己在controller方法中实现超时。
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)");
}
}
见 异步等待任务<T>超时完成。 了解更多关于超时等待的细节,包括当一个任务完成时取消 "其他 "任务。