I have .Net core 3.1 Web api project. There is a method to call separate API for getting some values.
This is my code
Define instance variable
CancellationTokenSource tokenSource = new CancellationTokenSource();
Looping through my values. There is separate method and user call the method when he wants.
foreach (var no in nos)
{
BackgroundJob.Enqueue(() => webServiceCall(no, tokenSource));//loop and give jobs to hangfire
}
WebServiceCall implementation
public async Task webServiceCall(int no, CancellationTokenSource tokenSource)
{
using (HttpClient client = new HttpClient())
{
try
{
var result = await client.GetAsync("https://localhost:44359/api/process/" + no, tokenSource.Token);
if (result.IsSuccessStatusCode)
{
var val = await result.Content.ReadAsStringAsync();
System.Diagnostics.Debug.WriteLine("Returned Value : " + val);
//logic code
}
}
catch (TaskCanceledException ex)
{
System.Diagnostics.Debug.WriteLine("HTTP Get request canceled." + ex.Message);
}
}
}
This code works and I need to cancel above batch from separate API. I try something like this and it doesn't work.
[HttpGet("CancellingJob")]
public async Task HagfireCancel()
{
tokenSource.Cancel();
}
But when I call to this, all processes happens as usual and did not canceled.... Can any one help me to cancel hangfire jobs?
I used Hangfire 1.7.0 version.