Async task jobs

I have an async method I want to start as a hangfire job. How I can launch it ?

Like that it doesnt work :

BackgroundJob.Enqueue(() => await ImporterService.ImportAllAsync()); 

Any idea ?

As for now, Hangfire deals only with synchronous method invocations. You can not use the await keyword inside your background methods. You should wrap your async method into another method to call it synchronously – http://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c.

Thanks for this question, I’ll make background job client to throw an exception for this case and think how to deal with async methods later. With correct implementation they can help to improve the processing throughput for I/O-bound background jobs.

Ah ok, it would be nice to support async methods yes.

Anyway I wrap my async methods inside another method like this :

public void MyMethod()
{
    MyMethodAsync().Wait();
}

What do you think about this way to call it ?

But what about exceptions, do you want to deep into AggregatedException details? It is better to check the link I posted above once again :smile:

Supporting Task methods would be the killer feature for me. I’m using Azure Web Jobs right now and have just discovered Hangfire (via a comment on the Web Jobs blog). My use cases mostly involve running jobs against Azure storage so there’s a lot of potential for async scale.

I already have a draft implementation of this feature, but it is in my head only. There are some difficulties related to continuations, worker threads and non-graceful shutdown. I’ll try to update the https://github.com/HangfireIO/Hangfire/issues/150 issue this weekend.

2 Likes

Hi @odinserj

I noticed this thread is 3 years old, but was async support for jobs ever implemented/completed?

Thread is old, but looks like https://www.hangfire.io/blog/2016/07/16/hangfire-1.6.0.html tells us it’s supported now