I am currently going through a .NET/React course and one lesson was about CancellationToken which I have seen before but never really understood.
I decided to take some notes.
Cancellation Token
A CancellationToken in .NET is a mechanism that lets you signal to running code that it should stop work, usually because the operation is no longer needed or the user canceled it.
It’s commonly used in asynchronous and long-running operations to make them cooperative and cancellable.
Usecase really depends on the queries we are making - how complex they are, how long they take, etc.
Testing CancellationToken
In my ActivitiesController I have passed a CancellationToken to the GetActivities method.
|
|
I can then use the CancellationToken in the GetActivityList.Handler mediator to check if the operation has been cancelled.
|
|
And verify the result inside console.
