How To Create A More Advanced Episerver Scheduled Task Systems

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.  Over the years, I've created many a scheduled task and they work well. 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:

[assembly: OwinStartup(typeof(Startup))]

namespace TSC.Website.Core.MVC
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
        }
    }
}

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:

  <add key="owin:appStartup" value="My.WebsitesNamespace.Startup" />
  

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.

            var connection = ConfigurationManager.ConnectionStrings["EPiServerDB"].ConnectionString;
            GlobalConfiguration.Configuration.UseSqlServerStorage(connection);

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.

            app.UseHangfireDashboard();
            app.UseHangfireServer();

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.

        public void Configuration(IAppBuilder app)
        {
            var connection = ConfigurationManager.ConnectionStrings["EPiServerDB"].ConnectionString;
            GlobalConfiguration.Configuration.UseSqlServerStorage(connection);

            var dashboardOptions = new DashboardOptions
            {
                Authorization = new[]
                {
                    new EpiserverAuthorizationFilter()
                }
            };

            app.UseHangfireDashboard("/episerver/backoffice/Plugins/hangfire", dashboardOptions);
            app.UseHangfireServer();
        }

Finally, you probably want an easier way to access your Hangfire dashboard, say from within the Episerver manu. You can use this class to add an 'Admin' tab with a link to the Hangfire dashboard.

    [MenuProvider]
    public class MenuProvider : IMenuProvider
    {
        public IEnumerable GetMenuItems()
        {
            var mainAdminMenu = new SectionMenuItem("Admin", "/global/admin")
            {
                IsAvailable = request => PrincipalInfo.Current.HasPathAccess(UriSupport.Combine("/AdminPage", ""))
            };

            var firstMenuItem = new UrlMenuItem("Hangfire", "/global/admin/main",
                "/episerver/backoffice/Plugins/hangfire")
            {
                IsAvailable = request => true,
                SortIndex = 100
            };

            return new MenuItem[]
            {
                mainAdminMenu,
                firstMenuItem
            };
        }

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.

Conclusion



Following the steps above, you now have a lot more secure Url to access Hangfire, you've restricted to only Episever admins and you've now got a handy link in your CMS menu.  Enjoy!

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top