In this article, let's learn about how to
free up resources with cancellation while accessing APIs
HTTP Client in .NET.
Note: If you have not done so already, I recommend you read the article on Improving performance and memory use while accessing APIs using HTTPClient in dotnet.
Table of Contents
- Advantages of Cancellation
- Cancellation Token Source and Cancellation
- Supporting Cancellation while reading data from API
Cancelling requests can be beneficial to
performance, both at level of
bandwidth consumption and
scalability. Let's learn how and I'll guide
you through this. We will start by learning why we should cancel requests when we no longer need them.
Imagine we're using HttpClient to get a data. Chances are that the
user of your application will navigate away
press a cancel button. In such a case, you want to be able to handle that instead of just letting the
request continue. The reasoning behind this is that behind the scenes,
HttpClient works with
async tasks. There's another reason, though. It's not unheard of for a
to happen. If that happens, we want to be notified of that so we can gracefully handle such a timeout. In such a case, it is due to the timeout
that the task gets canceled.
Advantages of Cancellation
It's important to know that cancelling a task that's no longer needed will
free up the thread that is used to
run the task. That means that
thread is potentially returned to the thread pool where it can be used for other work.
Save threadswhich can be used to handle other tasks / requests.
Save db callswhich can save compute cost.
Save datatransferred over network.
Cancellation Token Source and Cancellation
async tasks, a
CancellationTokenSource and a
CancellationToken are used. A
CancellationTokenSource is an
object, which manages and sends cancellation
notifications to the individual cancellation tokens. It exposes such a
CancellationToken via its token property.
CancellationToken is a lightweight value passed to one or more listeners, typically as a method parameter.
Listeners should then monitor the value of the
IsCancellationRequested property of that token and react accordingly.
From that, it follows that a
CancellationToken is what we need to pass to each task returning method we want to
listen to cancellation. So, the idea is that we provide these cancellation tokens to whichever part of our application we want to listen to cancellation
and cancel itself. And we use the
CancellationToken source to effectively request them to cancel themselves, which it
does by sending notifications to those tokens.
Supporting Cancellation while reading data from API
All we need to do now is to initialize a
CancellationTokenSource and set
a timeout. Then, we pass the token to the
We can then use the token to cancel the request if it takes too long. We can also use the token to cancel the request if the user navigates away.
Note: that for demo purpose we are cancelling after 10 seconds and we are awaiting for 5 seconds to simulate the API call failure. In real world scenario, you can set the timeout to 30 seconds or 1 minute or you can cancel when user presses cancel button.
Code Sample - Support Cancellation while Reading Data
Demo - Support Cancellation while Reading Data
Let's try Cancellation while reading data in this Demo, Click on the Read with Cancellation Button to see the demo on the screen. Note that data might not be rendered on the screen as the API is cancelled before the data is read. You can verify this from the browser console log.
In this article, we learned about how to
free up resources with cancellation while accessing APIs using
HTTP Client in .NET. We learned about what is
why we should cancel requests when we no longer need them and
cancellation. We also learned about
how to support cancellation while reading data from API.
I hope you enjoyed reading this article.