I have to stop the API execution through the Cancellation Token, In my method no for loops to check every time the cancellation requests, how to achieve it. I am using .NET core API
I have tried the below code
private string GetData(CancellationToken cancellationToken)
{
try
{
if (cancellationToken.IsCancellationRequested)
{
_cts.Cancel();
return "Task Cancelled";
}
if (condition)
{
//Business Logic
}
else
{
//Business Logic
}
}
catch (Exception e)
{
return e.Message.ToString(); ;
}
}
//Business Logic, probably at various places, terminating early if the token has been cancelled.