How can I cancel a database query in ASP.NET when the user's browser disconnects?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In modern ASP.NET, the usual way to cancel database work when the client disconnects is to propagate the request cancellation token into the database call. In ASP.NET Core that token is HttpContext.RequestAborted, and once it is passed into EF Core or ADO.NET async APIs, the database operation can be cancelled when the browser goes away.
Use the request cancellation token
In ASP.NET Core, every request has a cancellation token:
Model binding can supply the request token directly through the action parameter. You can also read it explicitly from HttpContext.RequestAborted.
The key point is that cancellation has to be passed down the stack. ASP.NET cannot magically interrupt a database driver call that never received a token.
EF Core example
With EF Core, most async query APIs accept a CancellationToken.
If the browser disconnects and the request token is cancelled, EF Core can propagate that to the provider.
Raw ADO.NET example
The same idea applies if you use SqlCommand directly.
Here again, cancellation only works because the token is passed into the async ADO.NET methods.
Classic ASP.NET versus ASP.NET Core
The best answer depends on the framework generation. In ASP.NET Core, RequestAborted is the normal path. In older ASP.NET, there were APIs such as Response.ClientDisconnectedToken, but the experience was less consistent and more framework-specific.
So if you are reading older advice, separate it by platform:
- ASP.NET Core: use
HttpContext.RequestAborted - classic ASP.NET: use the older disconnect token mechanisms available in that runtime
Do not mix the two models mentally.
Cancellation is cooperative, not magical
Even with the right token, cancellation is cooperative. The framework signals that the request has been aborted, but the lower layers have to honor it.
That means:
- use async APIs that accept tokens
- pass the token all the way down
- avoid wrapping long synchronous work that cannot observe cancellation
If part of the stack ignores cancellation, the query may keep running anyway.
What to do in repository or service layers
Do not stop at the controller. Accept a CancellationToken in service and repository methods too.
That keeps the cancellation signal alive through the whole request path.
Common Pitfalls
- Detecting client disconnects but never passing the cancellation token into the database call.
- Using synchronous database APIs that cannot cooperate with cancellation.
- Assuming ASP.NET will automatically kill a running SQL query with no support from the data provider.
- Mixing old ASP.NET guidance with ASP.NET Core request-cancellation patterns.
- Forgetting to thread the cancellation token through service and repository methods.
Summary
- In ASP.NET Core, use
HttpContext.RequestAbortedor a boundCancellationToken. - Pass that token into EF Core or ADO.NET async methods.
- Cancellation only works if the lower layers actually observe the token.
- Keep the token flowing through controllers, services, and repositories.
- Distinguish modern ASP.NET Core guidance from older classic ASP.NET APIs.

