I have a custom ASP.NET Core middleware and I want to retrieve the cancellation token for the request.
I tried to add it to the signature of the invoke like this:
public async Task Invoke(HttpContext context,CancellationToken token)
But as soon as I add it, it isn't called any more.
What am I doing wrong?
I think the idea is not to be able to cancel invocation of the middleware, from inside the middleware if you call some async task that accepts a cancellation token then you can create one and pass it in to what you are calling from inside there.
A common scenario would be to cancel a task if the request is aborted, so you could create the token like this:
CancellationToken CancellationToken => context?.RequestAborted ?? CancellationToken.None;
and then call some async service like getting data or querying the db, if the request is aborted then the cancellation request should happen