ASP.NET Core & Web API (50 Qs)•Part 1: Request Pipeline, Dependency Injection & Web APIs (1 - 10)
What is `CancellationToken` and why must you pass it to all async database and HTTP calls?
junior LevelMost AskedDependency InjectionPipesAsync & Promises
In Simple Words:
A CancellationToken notifies async operations that the client disconnected or the request timed out:
- If a user cancels an HTTP request or navigates away, passing
CancellationTokento EF Core (SaveChangesAsync(ct)) orHttpClientimmediately cancels the database query on the SQL server and releases the thread, preventing wasted database CPU cycles.