Home > front end >  How cancelling API request works internally in ASP .NET Core API?
How cancelling API request works internally in ASP .NET Core API?

Time:06-02

Suppose I have simple endpoint:

 [HttpGet]
 public async Task<ActionResult<string>> Get(CancellationToken cancellationToken)
 {
    // something...      
 }

And I send some http request via Angular: let sub = this.http.get<string>("some url").subscribe() and then before getting response from backend I unsubscribe: sub.unsubscribe()

My question is:

How come cancellationToken on backend have IsCancellationRequested set on true, even though when I monitor outgoing http request there are no more http requests passed to backend?

CodePudding user response:

I think you are assuming IsCancellationRequested is changed from the request initiator.

I do not think this is the case. Instead, I believe the value is actually changed internally by the API whenever the request is aborted or otherwise terminated, to signal to you that you should not waste anymore resources on a closed request. At least, that's my conclusion based on the only documentation I was able to find from Microsoft:

CancellationToken

Actions can optionally bind a CancellationToken as a parameter. This binds RequestAborted that signals when the connection underlying the HTTP request is aborted. Actions can use this parameter to cancel long running async operations that are executed as part of the controller actions.

  • Related