Hangin’ Around with Hangfire

.NET Core is awesome. Better performance, cleaned up interfaces, what’s not to like?

Well, in the cloud world some things just aren’t there yet. For example, if you want to use WebJobs for async processing you might have some problems with .NET Core (at least as of June 2018).

WebJobs provide a super easy way to do async processing. An example of this would be sending an email. Instead of just sending the email from a method in an MVC controller, you put a message in a queue. Then a second process picks up the message and sends it through an email system.

This reliance on async, or queued calls, is important for a well-designed system. Lots of operations performed by a system do not need to occur in real time. Using our email example above, queuing calls and eliminating an existing controller to send emails doesn’t make a lot of sense. There is no reason to make a controller operation wait on an email service.

Why not useWebJobs? With .NET Framework, I think WebJobs make a ton of sense when deploying to an Azure App Service. WebJobs are effectively the corollary to Web Apps. You use Web Apps to serve a web application, you use WebJobs to handle long-running async jobs that need to be handled by that web application.

The problem with WebJobs is that they are not currently well supported by .NET Core. I am sure this will change in the future, but as of right now getting a WebJob to deploy through VSTS is a battle. I recommend avoiding battles; go with what is easy.

That is why I have been using Hangfire, an open-source framework that helps create, process, and manage background jobs.

I would normally recommend WebJobs with Azure Web Apps, but until WebJobs are fully supported with .NET Core, Hangfire is a good option.

Here’s how to get started with Hangfire.

Install NuGet package “HangFire”.

Add Hangfire service in the ConfigureServices method (Startup.cs).

Configure Hangfire Server in the Configure method (Startup.cs).

Optionally add a dashboard. The Hangfire dashboard is a nice way to look beneath the hood.

You can add an optional authorization filter to the Hangfire dashboard that will limit dashboard access to only certain users.

Now to enqueue a Hangfire job, use the “Enqueue” method.

Hangfire might not be the first option I’d choose for async jobs in Azure. But given the current state of WebJobs support in Azure with .NET Core, Hangfire is a great option.

You can find information about Hangfire at https://www.hangfire.io.


Related posts