What's the difference between HttpContext.RequestAborted and CancellationToken parameter?
Asked Answered
B

1

20

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:

  1. Using the HttpContext.RequestAborted
  2. Using a CancellationToken parameter
  3. 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?

Bovine answered 7/11, 2017 at 8:50 Comment(2)
HttpContext.RequestAborted and injected CancellationToken should be the same, the source of the binder: github.com/aspnet/Mvc/blob/… (original thread: github.com/aspnet/Mvc/issues/5239#issuecomment-244924877). It does not work for me neither (at least when debugging).Kuchen
Update: by debugging I mean IIS Express and also I am using Core 1.1. According to the thread it should work with Core 2.0 and Kestrel.Kuchen
K
17

I know this is a 2 months old issue, but I have been struggling with this today too and came to some conclusions.


What is the difference and what should I use?

According to this thread these are exactly the same things. From CancellationTokenModelBinder source:

var model = (object)bindingContext.HttpContext.RequestAborted;

I can confirm, I always get the same values from HttpContext.RequestAborted and CancellationToken injection.

Advantage of HttpContext.RequestAborted is that it is available in all controller's methods, not just actions. The CancellationToken parameter is IMHO better readable but if you have a number of nested methods that need to react to the token, it may become impractical to propagate it through their parameters. I would just use the one that better suits your needs.


From the same thread, another post:

This only works in 2.0 not in 1.x

I know, this is not your case but it was mine.


Finally, still from the same thread and especially discussion here, there's a problem in IIS (specifically in its interface with Kestrel). AFAIK you have to use Kestrel and optionally (mandatory for 1.x) a reverse proxy such as IIS, Apache or Nginx. I believe this is your case.


Some quick testing: I created a project from Web API template using ASP .NET Core 2.0 and modified the first action in the controller it created:

// GET api/values
[HttpGet]
public IEnumerable<string> Get(CancellationToken cancelToken)
{
    Thread.Sleep(7000);
    cancelToken.ThrowIfCancellationRequested(); // breakpoint here
    return new string[] { "value1", "value2" };
}

Hit F5. It starts the app (including Kestrel) with IIS Express in front of it. Make the request and abort it (by closing a browser tab) before the 7 seconds. cancelToken.IsCancellationRequested is false when the breakpoint triggers.

Now, change the debug profile from IIS Express to WebApplication1 (or whatever it is called):

enter image description here

Everything works as expected for me.

I did also try the same with ASP .NET Core 1.1 but with no success.


AFAIK with ASP .NET Core 2.0, one should be able to use Kestrel on its own. I don't know if Apache or Nginx are doing better than IIS.

Kuchen answered 8/1, 2018 at 16:22 Comment(1)
You are right. I was also facing this issue. After reading your answer, I tried without using IIS Express and it worked perfectly as expected.Metzler

© 2022 - 2024 — McMap. All rights reserved.