【发布时间】:2023-08-07 22:16:02
【问题描述】:
据我所知,如果我在Startup.cs 中使用services.AddControllers() 或services.AddMvc()extension,“MVC 将自动绑定操作方法中的任何CancellationToken 参数。
我有以下TestController 和TestService 作为瞬态服务。
根据此信息,当自动绑定CancellationTokenIsCancellationRequested时,我作为参数传递的令牌也会被取消吗?
public class TestController : ControllerBase
{
private readonly ITestService _testService;
public TestController(ITestService testService)
{
_testService = testService;
}
[HttpGet, ActionName("Get")]
public async Task<IActionResult> GetAsync(CancellationToken cancellationToken)
{
await _testService.GetAsync(cancellationToken);
return Ok();
}
}
public class TestService : ITestService
{
public async Task GetAsync(CancellationToken cancellationToken)
{
//I also send the cancellationToken as a parameter to external API calls
}
}
【问题讨论】:
-
我有完全相同的问题,我提供的唯一解决方案是像您一样,将 cancelToken 作为参数传递给每个服务。他们将取消后续任务或 Linq to SQL。但我希望有一种更清洁的方法来做到这一点。
标签: asp.net-core controller asp.net-core-webapi cancellationtokensource cancellation-token