In this tutorial, you will learn how to use Hangfire as a replacement for Episerver scheduled tasks. Out of the box, Episerver provides a scheduled service solution to cater for any reoccurring tasks that you may need to perform; sending emails, seeding a cache, getting news items from a third party service. If you want to know more about Episerver scheduled tasks you can start here.
The scheduled service was written quite a long time ago and since then there's a new FREE kid on the block called Hangfire. Hangfire provides a similar function as an Episerver scheduled task manager, except it contains a lot more functionality; it's like the old scheduled task provider on steroids.. it even comes with a nice web portal that will allow you to keep track of tasks. Hangfire is free and it can be installed within your website within an hour. Hangfire also creates persistent tasks (by way of the database) so you can schedule jobs weeks or months in the future and be confident that they will run.
If Hangfire sounds like something that would benefit your project, then read on. In today's tutorial, I'm going to give you a simple step-by-step guide of how to install Hangfire with Episerver.
Adding Hangfire Into Your Project
Like most things .NET nowadays, Hangfire is installed via Nuget:
Simply, open package manager, search for Hangfire, install it. If you try and load your website straight away, you'll more than likely see this error:
'The following errors occurred while attempting to load the app. - No assembly found containing an OwinStartupAttribute.' Hangfire requires some slight configuration, so you will need to create a Startup class.
Add an OWIN Startup Class
Create a Startup.cs file in the root of your website and add the following:
Note the assembly attribute that creates a reference to itself, e.g. Startup => the class name Startup. In your app.settings within your web.config, you will need to make sure you have the following setting:
Where you tell .NET where your startup class is. If you picked up in my introduction, Hangfire creates persistent tasks.. this is done via SQL. As we're using Episerver we can use the Episerver database for this.
To configure Hangfire to use your Episerver database, you need to configure it with a connection string, this can be done with the code above.
Next, we need to enable Hangfire when your application starts, this is done using the snippet above.
If you followed all the steps, Hangfire should be ready to be loaded. You can load Hangfire (for now) using www.website.com/hangfire. Now the more astute of you will probably be asking, what so any unauthenticated user can see my scheduled tasks by adding 'Hangfire' onto my address.... this is where the Episerver side of configuration comes in.
Locking Hangfire Down
So we have two requirements to make Hangfire more secure:
You want only Episerver admins to have access to the dashboard
You want to make the Url to access the dashboard a little bit more secure.
Hangfire allows you to add authorization filters to the Hangfire settings. This means you can write some custom code to really lock down Hangfire if you want. I recommend locking access to the Hangfire dashboard down by Episerver administrator access only.
In the Startup classes code, you need to inject this setting. This means Startup now looks like this:
Finally, you probably want an easier way to access your Hangfire dashboard, say from within the Episerver menu. You can use this class to add an 'Admin' tab with a link to the Hangfire dashboard.
Boom! When you load Episerver you should now see something similar to this:
I won't go into too much detail about using Hangfire in this tutorial, but I'm hoping from the screenshots above you can see that the portal provides a lot more functionality than Episerver does.
Following the steps above, you now have a lot more secure Url to access Hangfire, you've restricted to only Episerver admins and you've now got a handy link in your CMS menu. Enjoy!