Is CancellationToken available in ASP.NET Core ActionFilter?
Asked Answered
G

1

5

I could inject CancellationToken into ASP.NET Core action method, but I would instead prefer to work with it using action filter. How to get access to cancellation token while implementing IAsyncActionFilter? My method should not have it as a parameter then.

Give answered 3/10, 2019 at 5:3 Comment(8)
@madreflection Please see an example here.Give
@madreflection It makes very much sense to inject/use CancellationToken values and supply such down through call-stacks. They contain “internal magic” to all wire to the same CancellationTokenSource, as an example; not so primitive as an integer, eg. Being a struct is a separate discussion point. (The injected value would be discreet per request or as required, and usually not a singleton: again, this is a separate discussion as to the particular injection method and naught to do with value types.)Gemmulation
I think that "inject" is a wrong word here. Instead you may say "I could pass CancellationToken into ASP.NET Core action method..."Littman
@Littman I do not think that I could pass something I do not have. CancellationToken is being instantiated and provided by interface injection as an action method argument by ASP.NET Core the same way as some IoC container could do constructor injection of other dependencies it is responsible for.Give
@DmitryNogin, trust me, you have it. As from your link: "MVC will automatically bind any CancellationToken parameters in an action method to the HttpContext.RequestAborted token, using the CancellationTokenModelBinder. This model binder is registered automatically when you call services.AddMvc() (or services.AddMvcCore()) in Startup.ConfigureServices()."Littman
@Littman Yes, that could work. But as I stated above at the end of the original question: I actually would not like to have such a CancellationToken parameter in every method. I am looking for a way to access and process token by the action filter only. (It is supposed to work with this).Give
As from your link, you need to get context.HttpContext.RequestAborted token within your IActionFilter.OnActionExecuting(ActionExecutingContext context) methodLittman
@Littman Perfect, thanks! Shame on me :) I just googled it over the course of the discussion to have a code example for sharing and did not actually read it :) Could you please post your answer below so I could accept it?Give
L
16

You already post a link to a very good article, which contains a small hint where you can get this token.

MVC will automatically bind any CancellationToken parameters in an action method to the HttpContext.RequestAborted token, using the CancellationTokenModelBinder.

So, all you have to do is acquire that token in your action filter:

public class CustomActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var cancellationToken = context.HttpContext.RequestAborted;
        // rest of your code
    }
}
Littman answered 3/10, 2019 at 7:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.