Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke async methods in Hangfire?

I have asp.net core API application and this is the first time i will be using HangFire.

In .Net Core application all my methods are async. Based on SO Post it's not good idea to use wait() while calling async method in hangfire.
Also as per the hangfire support issue in v1.6.0, async support was added. I am using version 1.6.12 but still i dont see async support.

How do i call async method from Enqueue. Currently i am using wait()

public class MyController : Controller {     private readonly Downloader _downlaoder;     private readonly IBackgroundJobClient _backgroungJobClient;     public MyController(Downloader downloader, IBackgroundJobClient backgroungJobClient)     {         _downlaoder = downloader;         _backgroungJobClient = backgroungJobClient;     }      [HttpPost]     public void Post([FromBody]IEnumerable<string> files)     {         _backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files).Wait());     } } 
like image 697
LP13 Avatar asked Mar 29 '17 20:03

LP13


People also ask

Does hangfire support async?

Hangfire 1.6 just released with experimental . NET Core platform support, deep integration with ASP.NET Core and async / await programming model support. Start processing background jobs with ease even on Linux and OS X today!

Can we call async method in sync method?

Solution A If you have a simple asynchronous method that doesn't need to synchronize back to its context, then you can use Task. WaitAndUnwrapException : var task = MyAsyncMethod(); var result = task. WaitAndUnwrapException();

What is Hangfire?

Hang fire refers to an unexpected delay between the triggering of a firearm and the ignition of the propellant. This failure was common in firearm actions that relied on open primer pans, due to the poor or inconsistent quality of the powder. Modern firearms are also susceptible.

Is it possible to call async method in Hangfire?

In .Net Core application all my methods are async. Based on SO Post it's not good idea to use wait () while calling async method in hangfire. Also as per the hangfire support issue in v1.6.0, async support was added.

Is it possible to use await in Hangfire?

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

How do I call a method in the background in Hangfire?

Hangfire takes regular classes and regular methods to perform them in the background, because it is simple: BackgroundJob.Enqueue( () => Console.WriteLine("Hi!")); This snippet says that the Console.WriteLine method will be called in background. But note that the name of the method is Enqueue, and not the Call, Invoke and so on.

What is methodinfo in Hangfire?

Before creating a background job, Hangfire serializes the information about the given method (its type, method name and parameter types) to a string. MethodInfo serialization process is invisible to a user, unlike the arguments serialization.


1 Answers

Based on one of the examples on the repository on github

Just remove the Wait blocking call

_backgroungJobClient.Enqueue(() => _downloader.DownloadAsync(files)); 

The method knows now how to handle Func that returns Task

Hangfire 1.6.0 - Blog

The enqueueing logic is the same for sync and async methods. In early betas there was a warning CS4014, but now you can remove all the #pragma warning disable statements. It was implemented by using Expression<Func<Task>> parameter overloads.

BackgroundJob.Enqueue(() => HighlightAsync(snippet.Id)); 

Note:

That’s not a real asynchrony

Please consider this feature as a syntactic sugar. Background processing hasn’t became asynchronous. Internally it was implemented using the Task.Wait method, so workers don’t perform any processing, while waiting for a task completion. Real asynchrony may come in Hangfire 2.0 only, and it requires a lot of breaking changes to the existing types.

like image 132
Nkosi Avatar answered Sep 25 '22 16:09

Nkosi