SITECORE WITH HANGFIRE

What’s Hangfire? 



https://www.hangfire.io/

It is an easy way to perform background processing in .NET and .NET Core applications. No Windows Service or separate process is required. 


Reasons to use Hangfire

More information on Hangfire can be found on its official documentation: Hangfire – Background jobs and workers.

Integration with Sitecore

We first need to download the Hangfire NuGet packages into our solution. The required packages are as follows:

Install-Package Hangfire
Install-Package Hangfire.Core
Install-Package Hangfire.SqlServer
Install-Package Hangfire.AspNet

Since we need to initialize the Hangfire in the Sitecore OWIN, you will need to have a reference to the Sitecore.Owin.dll. You can install it via the NuGet package.

Once you have those packages installed, you will need to implement an OWIN middleware that will register the Hangfire with its corresponding configuration. Below is the code of the integration

using System;
using System.Collections.Generic;
using Hangfire;
using Hangfire.SqlServer;
using Sitecore.Owin.Pipelines.Initialize;
public class HangfireInitializer : InitializeProcessor
{
public override void Process(InitializeArgs args)
{
var app = args.App;
app.UseHangfireAspNet(GetHangfireServers);
app.UseHangfireDashboard("/sitecore/hangfire", new DashboardOptions
{
Authorization = new[]
{
new HangfireAuthorizationDashboard()
}
});
}
private IEnumerable<IDisposable> GetHangfireServers()
{
GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage("MyConnectionStringName", new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
});
yield return new BackgroundJobServer();
}
}

As you will see above, the implementation of the GetHangfireServers method will configure the connection to the SQL. Note that you may need to change the configuration as per your requirement. You may have more information at Hangfire documentation.

I have implemented a custom dashboard option. This allows you to configure if the user should be authenticated before accessing the dashboard. The code can be found below.

using Hangfire.Dashboard;
using Microsoft.Owin;
public class HangfireAuthorizationDashboard : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
var owinContext = new OwinContext(context.GetOwinEnvironment());
return Sitecore.Context.User.IsAuthenticated;
}
}

The above code is just checking if the Sitecore context user is authenticated or not. If you are authenticated, then you will be able to access the Hangfire dashboard. You may want to add the logic from the Sitecore Identity authentication.

Update Sitecore configuration files

The first update you need to perform is to add the custom OWIN middleware we just implemented. The configuration file will look as follows:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/&quot; xmlns:role="http://www.sitecore.net/xmlconfig/role/&quot; xmlns:search="http://www.sitecore.net/xmlconfig/search/"&gt;
<sitecore>
<pipelines>
<owin.initialize>
<processor type="YourNamespace.HangfireInitializer, YourAssemblyName" />
</owin.initialize>
</pipelines>
</sitecore>
</configuration>

One important update you need to have is to ignore the path /sitecore/hangfire else Sitecore will interpret it as a friendly URL. You do so by adding the path to the IgnoreUrlPrefixes settings in Sitecore.config. You may want to create your own patch file and add it, which is as follows:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/&quot; xmlns:role="http://www.sitecore.net/xmlconfig/role/&quot; xmlns:search="http://www.sitecore.net/xmlconfig/search/"&gt;
<sitecore>
<settings>
<setting name="IgnoreUrlPrefixes" value="/sitecore/hangfire|/sitecore/default.aspx|/trace.axd|/webresource.axd|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.DialogHandler.aspx|/sitecore/shell/applications/content manager/telerik.web.ui.dialoghandler.aspx|/sitecore/shell/Controls/Rich Text Editor/Telerik.Web.UI.SpellCheckHandler.axd|/Telerik.Web.UI.WebResource.axd|/sitecore/admin/upgrade/|/layouts/testing|/sitecore/service/xdb/disabled.aspx" />
</settings>
</sitecore>
</configuration>
view rawgistfile1.txt hosted with ❤ by GitHub

Testing and Validation of the Hangfire Integration

Compile the solution and deploy both code and config files. Once completed, load your Sitecore instance and login into the Sitecore CMS. As soon as you are logged in, you can access the Hangfire Dashboard by navigating to the URL [domain]/sitecore/hangfire.

One quick step you can do is to add a Launchpad button in Sitecore so that it comes much easier to access the dashboard. All you need to do is

  1. Switch to the core database
  2. Navigate to the path /sitecore/client/Applications/Launchpad/PageSettings/Buttons. Then choose in which category you want to add the button. In my case, I added it to Tools
  3. Insert a LaunchPad-Button item
  4. Add the name, icon etc. Most important one is the field name Link. You can add a full URL or just a relative path. Example, /sitecore/hangfire

Upon clicking on the button, it will open the Hangfire dashboard. You can choose to either open it in a new tab or current window from the LaunchPad-Button item you created in the core database

Comments