Use .Net CancellationTokens

April 9, 2024    Development .Net

Use the CancellationToken in your .Net Applications

In .Net, the MVC/API Controller gives a CancellationToken. You should use this and pass it to the HttpClient inside of our Http package.

See Recommended patterns for CancellationToken - Developer Support

“Long running requests and cancellation

Have you ever been on a website where you’ve made a request for a page, and it just sits there, supposedly loading? Eventually you get board and click the “Stop” button, or maybe hammer F5 to reload the page. Users expect a page to load pretty much instantly these days, and when it doesn’t, a quick refresh can be very tempting.

That’s all well and good for the user, but what about your poor server? If the action method the user is hitting takes a long time to run, then refreshing five times will fire off 5 requests. Now you’re doing 5 times the work. That’s the default behaviour in MVC - even though the user has refreshed the browser, which cancels the original request, your MVC action won’t know that the value it’s computing is going to be thrown away at the end of it!”

Using CancellationTokens in ASP.NET Core MVC controllers

“Consider using the CancellationToken, but do so with caution:

Utilize the CancellationToken for read-only actions that don’t impact data integrity.

Apply the CancellationToken when your action is genuinely cancellable. Once you reach the ”point of no return,” you must decide whether to roll back your actions or proceed with a CancellationToken. None for irreversible actions.

Use a CancellationToken to release resources from resource-intensive processes. Establish checkpoints where transactions can be avoided—why respond if there’s no recipient on the other end?

DO NOT use a CancellationToken if there’s a risk of compromising data integrity.”

Mastering CancellationToken in Managed Threads for .NET

Tasks needed to update existing code

  • Update the call to the HttpClient to accept CancellationToken as a new method/overload
  • Update Controller methods, then pass that to the clients
  • Update internal methods that use HttpClient directly.

There would be a lot of code changes, but should result in a more performant and reliable system.

Remind others with Analyzers

Microsoft has one built in CA2016: Forward the CancellationToken parameter to methods that take one .

Meziantou developed one . “The Roslyn Analyzer I develop — Meziantou.Analyzer — contains a rule to detect missing CancellationToken: MA0040 - Use a cancellation token when available.”

Now you need to convince your current and future developers to stop ignoring warnings. 😅

More Information

Watch the Story for Good News
I gladly accept BTC Lightning Network tips at [email protected]

Please consider using Brave and adding me to your BAT payment ledger. Then you won't have to see ads! (when I get to $100 in Google Ads for a payout, I pledge to turn off ads)

Use Brave

Also check out my Resources Page for referrals that would help me.

Swan logo
Use Swan Bitcoin to onramp with low fees and automatic daily cost averaging and get $10 in BTC when you sign up.