Often on a website build, you'll need a way to manage background tasks. When you need to do things like scheduling orders, sending emails, refreshing data from an external feed, then you'll need a way to call code on a frequent basis. In the old days in order to create a scheduled task, you would need a Windows service, however, as times moved and cloud hosting and web apps have become more popular, elastic deployments now mean we need new ways of handling this scheduling... this is where hangfire comes in.
Hangfire is a free and open-source framework that will allow you to easily perform background processing in .NET and .NET Core applications. With Hangfire on Windows Service or separate process required. Hangfire is backed by persistent storage so you can do IIS resets and not lose your data.
If you need to use a scheduler, then I'm hoping I've convinced you that Hangfire is a good option. In this tutorial, I'm going to cover everything you need to install Hangfire, lock it down so none of your web visitors can't access it AND make it appear within the Umbraco backend. So let's begin!
How To Install Hangfire
Getting Hangfire integrated within your Umbraco website a vanilla entity is easy.
You can install Hangfire via the NuGet Package Manager in Visual Studio. On your web project, right-click and select 'Manage Nuget Packages', type in 'Hangfire' and then add it to your project.
As Hangfire uses persistent storage, e.g a SQL database, we'll need to define a connection string to use with it. As we're using Umbraco we can simply use the Umbraco one, that I'm assuming you've already set-up.
The next step to get Hangfire up and running is modifying your Startup.cs file and provide the necessary connection string and routing information. As you're using Umbraco you may have to create the Start.cs yourself, so don't be surprised if you don't have one in your solution.
Ok, so the important things to note from this snippet are.
The assembly attribute. The typeof() needs to point to your Startup file, this will be your websites assembly namespace.
Inherit from 'UmbracoDefaultOwinStartup'
Inherit from 'UmbracoDefaultOwinStartup'Umbraco.Core.ApplicationContext.Current.DatabaseContext.ConnectionString
We're defining the route we will access hangfire within Umbraco. To get a custom MVC view, or, in this case, a hangfire page to display within the Umbraco backend, it will need to be accessible within '/Umbraco/backoffice/Plugins/'
We're adding 'UmbracoAuthorizationFilter' a custom class we'll define next.
Creating The IDashboardAuthorizationFilter
I'm hoping the code for this is self-explanatory. It's checking if the current user has an Umbraco Authorization ticket set. If someone is logged into Umbraco they can then access it.
On the appSettings section of the web.config, make the following changes:
Open the web.config (or app.settings.config) and make sure 'owin:appStartup' is pointing to the right location.
Make Hangfire Display In Umbraco
In the example, you should see the hangfire option within your content section, called 'Event Handling' with a node called Hangfire. In my website I've used a custom section, to make it easier. If you want to know how to do this, you can read 'How To Create a custom section in Umbraco 7'
When you open the dashboard for the first time, Hangfire should create all of its database tables for you. In your SQL database, you will see the following new tables, AggregatedCounter, Counter, Hash, Job, JobParameter, JobQueue, List, Schema, Server, Set, and State.
Creating the code to create add a hangfire task can be done in two ways:
- A one-off fire-and-forget request in Hangfire, using Enqueue()
BackgroundJob.Schedule(() => Console.WriteLine("One Off Task."), TimeSpan.FromMilliseconds(1000));
- A regular reoccurring task, using the RecurringJob class, specifically with the AddOrUpdate() method
RecurringJob.AddOrUpdate(() => Console.WriteLine("Repeat Daily"), Cron.Daily());