AspCore Custom Middleware with Undo Token

When I have a regular asp intermediate core, I want to get a cancel token for the request

I tried adding it to the call signature as follows:

public async Task Invoke(HttpContext context,CancellationToken token) 

But as a son, as I add him, he is no longer called.

What am I doing wrong?

+5
source share
1 answer

I think the idea is not to cancel the middleware call from inside the middleware, if you call some asynchronous task that accepts the cancel token, then you can create it and pass it to what you are calling from the inside.

A common scenario would be to cancel the task if the request was aborted, so you could create a token like this:

 CancellationToken CancellationToken => context?.RequestAborted ?? CancellationToken.None; 

and then call some async service, for example, to get data or request db, if the request is canceled, then a cancellation request should occur

+6
source

All Articles