You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace Polly8Study.WebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]/[action]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class TimeoutController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<TimeoutController> _logger;
|
|
|
|
|
public TimeoutController(ILogger<TimeoutController> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 快速接口
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult Fast()
|
|
|
|
|
{
|
|
|
|
|
return Ok("Fast");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 慢接口:耗时约2秒
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public async Task<IActionResult> Slow(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
//使用“异常过滤器”, 全局统一处理
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(1000*2, token);
|
|
|
|
|
|
|
|
|
|
return Ok("Slow");
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("用户取消操作:" + ex.Message);
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
|
|
|
|
|
return Problem("服务器异常");
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("请求结束");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|