我正在尝试为ASP.NET Core 2.0创建一个异步视图组件。当用户离开页面时,它将执行应取消的操作。我有以下选择:
- 使用 HttpContext.RequestAborted
- 使用 CancellationToken 参数
- 我也可以链接令牌
选项 1 如下所示:
public class AmazingMessageViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(string text, int wait)
{
//uses request aborted
await Task.Delay(wait, HttpContext.RequestAborted);
return View<string>(text);
}
}
选项 2 如下所示:
public class AmazingMessageViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(CancellationToken cancellationToken, string text, int wait)
{
await Task.Delay(wait, cancellationToken);
return View<string>(text);
}
}
这两个操作都不适用于 Kestrel(看起来像一个错误)。在这两种情况下,令牌都被填充(可能是因为结构?)
有什么区别,我应该使用什么?