Sitefinity has great built-in options for scheduling your background tasks, but for those times you need more, it’s also easy to integrate external tools. Here’s how you can improve your task scheduling in Sitefinity with Hangfire.
As a web developer, you may face different scenarios which require using scheduled background tasks. The scenarios may vary from obtaining data from external or internal source (e.g. Database, CRM, ERP etc.), modifying it and storing it again to just triggering some custom logic every minute, day, week etc.
First let's see what is common for both approaches. Both:
Now let's see the differences.
Given this, it is recommended to use the built-in Sitefinity Scheduler for small numbers or similar types of scheduled tasks, when you don’t really require a management UI for them.
Hangfire is convenient when you need advanced scheduling configuration (e.g. CRON syntax) and have to manage the jobs using UI.
Now let's see how to integrate Hangfire in Sitefinity. There are 2 simple steps:
In order to use Hangfire with your Sitefinity application you need to install the Hangfire NuGet package and all its required dependencies:
NOTE: Sitefinity 11 references Owin and Newtonsoft.Json versions which are higher than the default ones used by Hangfire. Make sure to update all NuGet packages so they correspond to the versions referenced by Sitefinity. To do this you can execute the following commands in the NuGet Package manager console:
Update-Package Owin -Version 3.1.0
Update-Package Owin.Host.Systemweb -Version 3.1.0
Update-Package NewtonSoft.Json -Version 9.0.1
Once you have installed the needed NuGet packages you can proceed with configuring Sitefinity to use Hangfire.
Hangfire is based on OWIN middleware and Sitefinity provides a clean and easy integration.
The first step is to create a class and name it Startup (if such a class already exists it can be used). This class is used as an entry point by OWIN which looks for a Configure() method to execute upon the application startup.
There is already a great article about integrating OWIN in Sitefinity which can be found here.
public class Startup
public void Configuration(IAppBuilder app)
// Registers the default Sitefinity middlewares
// Configures the SQL Server connection for Hangfire
// Registers Hangfire background job server
// Registers Hangfire dashboard UI under the specified path
// and provides an authorization filter
// Creates a recurring background job
RecurringJob.AddOrUpdate(() => Log.Write(
"Long-running job executed by Hangfire."
"* * * * *"
// Creates a scheduled background job
BackgroundJob.Schedule(() => Log.Write(
"Scheduled job executed by Hangfire."
, ConfigurationPolicy.Trace), TimeSpan.FromMinutes(5));
private string connectionStringName =
The code snippet above attaches Sitefinity middleware to the app. This is needed in order to turn on Sitefinity OWIN capabilities. Then we register the SQL Server storage by passing the name of the connection string which is used for persisting the background job data. For UI visualization the Hangfire dashboard middleware is also registered. To secure the dashboard, we set an authorization option which prevents unauthorized access. Finally, there are two background jobs created:
The connection string for Hangfire can be added in the web.config file:
"data source=.\SQLSERVER;Integrated Security=SSPI;initial catalog=SitefinityDB"
In our case we are using the same database were the Sitefinity data is persisted.
In addition to creating a Startup class it is also important to add an entry in the web.config file to make your application aware of the OWIN entry point:
Lastly, we need to secure the Hangfire dashboard so that only users with administrative rights can view it. Failing to do so can pose a serious security threat to our web application. Fortunately, Hangfire provides us with an easy way of creating an authorization filter to accomplish just that.
internal class SitefinityAuthorizationFilter : IDashboardAuthorizationFilter
public bool Authorize(DashboardContext context)
// Retrieve the Sitefinity identity of the current users
currentIdentity = ClaimsManager.GetCurrentIdentity();
// Checks whether the Sitefinity identity has unrestricted access
In the code snippet above we create a SitefinityAuthorizationFilter class by implementing the IDashboardAuthorizationFilter interface. Next, we implement the Authorize() method in which the current user’s identity is retrieved by calling the ClaimsManager.GetCurrentIdentity() method. Finally, we return the IsUnrestricted property which checks whether the Sitefinity identity of the user has unrestricted access to the website (e.g. if the user is an administrator).
Let's take a look under the hood of Hangfire. The picture below shows the lifecycle of a job.
In advanced software architecture, you may want to use IoC containers such as ObjectFactory in Sitefinity. Hangfire uses JobActivator to instantiate the target types and allows you to implement a custom job activator, like for example ObjectFactoryJobActivator. More information about how to implement it can be found here.
Sitefinity provides developers with an easy and simple way for integrating any pluggable middleware that adheres to the OWIN concept.
In addition to Sitefinity’s built-in capabilities, integrating Hangfire into Sitefinity is easy and makes it possible to easily extend and modify the job scheduler.
Want to learn more about what Progress Sitefinity can do? Feel free to request a demo today.
Nader Dabour is a senior developer at Progress. He has been with the Sitefinity team since 2014 and has worked on various domains including technical documentation, infrastructure, bug fixing, development of internal tools, product features and public web portals. He is passionate about Sitefinity and all things web.
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
Copyright © 2019 Progress Software Corporation and/or its subsidiaries or affiliates.All Rights Reserved.
Progress, Telerik, Ipswitch, and certain product names used herein are trademarks or registered trademarks of Progress Software Corporation and/or one of its subsidiaries or affiliates in the U.S. and/or other countries. See Trademarks for appropriate markings.