In this tutorial, you will learn how you can use Hangfire with Episerver CMS. Out of the box, Episerver provides a scheduler to cater for re-occurring tasks. This mechanism can be very useful for simple tasks, however, it does have limitations. For one, it is not possible to debug tasks from within the UI, instead, you will need access to the logs. A second issue is that it is not very easy to see a global view of all the tasks that are currently enabled and scheduled. For a few simple tasks the EPiserver scheduled will do, for anything more complex use Hangfire.

Hangfire in Episerver

On most projects, if I need to build a lot of scheduled tasks then my personal preference is to spend an extra hour of dev effort by installing Hangfire. Hangfire is free, it is more feature-rich than the Episerver scheduler, it comes with a dashboard that gives better visibility on what tasks are scheduled, when and what has passed, and failed. If you think this sounds great and you want to use Hangfire on your project, I have written an Episerver installation guide here.

Since that last article, I've come up with a few more ideas that I think will help you get more from the product. Tweaks on how to render more descriptive data within the dashboard when your tasks run. For this tutorial, let us create a very crude task that will render a content ID:

The code to add this task into the hangfire queue, would look like this:

IBackgroundJobClient is a hangfire API. To add something to the queue we use Schedule(), passing our custom class MyTask in as T. The code above will trigger the code within RenderContentId() when executed. This code is simple and great, however, if you looked within the Hangfire completed job queue, you will notice that the name of the executed task is very undescriptive. The display name Hangfire uses is determined using reflection. Hangfire simply renders the class and method name of the executed task. There is no reference to the values passed in, or out.

This can be annoying in certain circumstances. What happens if you had a job to reindex all the content within the website? Each page being individual added into the queue. How could you tell what content has been scheduled and what hasn't? In the completed job queue you would only see a list of completed tasks that all look identical!

When working with Episerver and Hangfire, I recommend appending the job name with some unique values so that you can identify it easily. In this example, adding the content ID to the name within Hangfiure would be super useful. To do this you can use the DisplayName attribute. By decorating the method in our scheduled task with this attribute you can configure the name Hangfire renders, like so:

NOTE: The {0} relates to the parameters being passed into the task. In this instance, I am only passing in one parameter, the ID. If I wanted to use a second parameter, I could render the second parameter in the job name like this:

This approach will make it a lot easier to figure out what is going on within Hangfire. Another useful tip is this also works on interfaces. If you use an interface to schedule a task, you can also decorate the interface with DisaplyName.

Another useful tip when using Hangfire with Episerver is to check the queue to see if the content you are working with is either already scheduled or, has been processed recently. This can be done using Hangfires JobStorage API. To check the queue for a tasks, you can use the following code:

This code checks the default queue to see if any jobs with a matching ID are scheduled. On the second line, within EnqueuedJobs(), the second argument is the start location in the index. If you want to start at the most recent entry set this to 0. The last parameter is the number of jobs to check. In this example, the API will only search 10 records. The larger the value you add here, the longer the check will take. Continuing with our page processing example, if you wanted to check that the home page has recently been processed by Hangfire, you can check the succeeded job queue like this:

Armed with these things you should be able to use Hangfire like a pro,. Happy Coding!