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.
|Sitefinity Built-in Scheduler||Hangfire|
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.
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:
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.
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.
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.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.Learn More
Subscribe to get all the news, info and tutorials you need to build better business apps and sites
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.