I recently started using Hangfire to handle my background jobs in an ASP.NET project.
My jobs are involving lots, lots (and lots) of HTTP calls using WebRequest
(I may migrate to something else, but that's for later).
While browsing SO on this subject, I stumbled upon people who do async calls in hangfire.
Is there any point in using async/await
tasks inside a Hangfire job ? I'm aware that it is a queue, so jobs won't execute concurrently, but since my project has no GUI, is there any reason I would use async/await
to do my HTTP calls inside a Hangfire job ?
PS : The requests that are involving a long-running job use the Task "design pattern", so clients won't experience any timeout
You can absolutely use Hangfire to run async methods, and use awaits within those async methods.
The SO question you linked to was making a blocking Wait
call on the method passed to Hangfire to be serialized and then later executed by the hangfire service.
This is very different from something like
hangfireClient.Enqueue(() => myClass.RunAsync());
where RunAsync
is an async
method that contains awaits
.
public async Task<bool> RunAsync() {
//do your work
var subJob = new worker();
return await subJob.DoWork();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With