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.

59 lines
1.4 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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("请求结束");
}
}
}
}