I'm trying to create an async view component for ASP.NET Core 2.0. It will do an action that should be cancelled when the user navigates away from the page. I've got the following options:
- Using the HttpContext.RequestAborted
- Using a CancellationToken parameter
- I could also chain the tokens
Option 1 looks like this:
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);
}
}
Option 2 looks like this:
public class AmazingMessageViewComponent : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync(CancellationToken cancellationToken, string text, int wait)
{
await Task.Delay(wait, cancellationToken);
return View<string>(text);
}
}
Both action do not work with Kestrel (looks like a bug). In both cases the tokens are filled (maybe because of struct?)
What is the difference and what should I use?