答案 0 :(得分:0)
您可以在控制器方法中自己实现超时:
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,包括在完成时取消“其他”任务。