Setting up Hangfire in an ASP.NET Web API Application
If you want to run some background tasks inside your ASP.NET Web API application, Hangfire is one of those libraries you should have a look at. You don’t need a separate Windows Service or anything, you can just spawn the task within the Web API. Having said that, Hangfire gives you the extended functionality to run your background task as a Console Application or as a Windows Service. For the persistence storage for the tasks, Hangfire uses SQL Server, MySQL, Redis, etc.
In this article let’s see how you can setup Hangfire with an ASP.NET Web API application. For that, let’s start off by creating an ASP.NET Web API project.
After the project is created install Hangfire using Package Manager Console by simply running the Install-Package Hangfire command. Alternatively, you can use NuGet Packet Manager to install Hangfire.
https://lh3.googleusercontent.com/-z9joOOXaI4g/V2oTcpsu5yI/AAAAAAAAECo/lwpSzbfVoII/image_thumb8.png?imgmax=800 |
Install-Package Hangfire |
Now you have all the necessary dependencies installed. Let’s jump into configuring Hangfire. Open up Startup.cs and modify the code as follows.
using Hangfire;
using Microsoft.Owin;
using Owin;
using System.Configuration;
[assembly: OwinStartup(typeof(HangfireDemo.Startup))]
namespace HangfireDemo
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
GlobalConfiguration.Configuration.UseSqlServerStorage(ConfigurationManager.ConnectionStrings["HangfirePersistence"].ConnectionString);
app.UseHangfireDashboard();
app.UseHangfireServer();
}
}
}
Here you must specify the Hangfire persistence connection string. Here the connection string is specified in the web.config and a blank database is created as in the connection string in the SQL Server instance. app.UseHangfireDashboard() will setup a dashboard in http://<rootsiteurl>/hangfire for you to have a look at your jobs. app.UseHangfireServer() will setup a new instance of BackgroundJobServer. Now let’s just run the application.
You will see that a set of tables has been created in the Database which you have mentioned in the persistence connection string.
https://lh3.googleusercontent.com/-UTS4njLPO8A/V2oTejcMy7I/AAAAAAAAEC4/zR_cnGSaFTs/image7_thumb.png?imgmax=800 |
Hangfire Persistence Related Tables |
Now let’s navigate to http://<rootsiteurl>/hangfire in the browser.
https://lh3.googleusercontent.com/-XKfx_itewyc/V2oTgNmImFI/AAAAAAAAEDI/PxcE5sgvC3M/image11_thumb.png?imgmax=800 |
Hangfire Dashboard |
As you can see, you will be provided with a nice little Dashboard where you can find important things such as Jobs, Retries, etc.
Hangfire basically provides the ability to create following three types of tasks:
- Fire-and-forget tasks
- Delayed tasks
- Recurring tasks
For the demonstration purposes, let’s just create a Recurring Task which will print something to the output window every minute. What you need to do is just modify the Startup.cs adding the following line just below the app.UseHangfireServer().
RecurringJob.AddOrUpdate(() => Debug.WriteLine("Minutely Job"), Cron.Minutely);
And if you run the Web API application now, you can see the following in output window. “Minutely Job” will be written to the output window every minute.
https://lh3.googleusercontent.com/-LMoa-Fyyx7U/V2oThg4JBwI/AAAAAAAAEDY/dnq6RCXqjwg/image_thumb1.png?imgmax=800 |
Debug Output |
Now If we have a look at the dashboard, we can see important information related to the job we just created.
https://lh3.googleusercontent.com/-agF8vfbobCw/V2oTi1Nr0yI/AAAAAAAAEDo/upJR4YOH-ww/image_thumb7.png?imgmax=800 |
Hangfire Dashboard - Recurring Jobs |
https://lh3.googleusercontent.com/-BosEJwIPSUE/V2oTk5N-DTI/AAAAAAAAED4/G65qCQeUlJw/image_thumb6.png?imgmax=800 |
Hangfire Dashboard - Succeeded Jobs |
So that’s it. You can find rich documentation about Hangfire from their site. Do explore and high five to all the developers in Hangfire.
Happy Coding.