Take advantage of Hangfire, an open source job scheduling framework, to schedule fire-and-forget, recurring tasks in Web applications sans the need of a Windows Service Scheduling jobs in Web applications is a challenge, and you can choose from many frameworks for the task. A popular open source library, Hangfire is one framework that can be used for scheduling background jobs in .Net. Why should I use Hangfire? There are many job scheduling frameworks available today. Why then should you use Hangfire instead of, say, Quartz.Net, which is another popular framework that has long been in use? Well, one of the major drawbacks of Quartz.Net is that it needs a Windows Service. On the contrary, you don’t need a Windows Service to use Hangfire in your application. The ability to run without a Windows Service makes Hangfire a good choice over Quartz.Net. Hangfire takes advantage of the request processing pipeline of ASP.Net for processing and executing jobs. Note that Hangfire is not limited to Web applications; you can also use it in your Console applications. The documentation for Hangfire is very detailed and well structured, and the best feature is its built-in dashboard. The Hangfire dashboard shows detailed information on jobs, queues, status of jobs, and so on. Getting started To create a new project in Visual Studio that leverages Hangfire, follow these steps: Open Visual Studio 2015 Click on File > New > Project Select Visual C# > Web from the list of the project templates displayed Select ASP.Net Web application from the list of the Web project templates Save the project with a name The next step is installing and configuring Hangfire in your application; the process is quite straightforward. You can install Hangfire via the NuGet Package Manager in Visual Studio. Alternatively, you can also use the Package Manager Console to install the Hangfire library. The default installation of Hangfire uses SQL Server for storing scheduling information. Additionally, you can install Hangfire.Redis if you use Redis instead for storage. Note that Hangfire stores your jobs in a persistent storage — you need to configure the storage before you start using Hangfire. To do this, create a database and specify the database credentials in the connection string in the configuration file. You don’t need to create the tables in your database; Hangfire will do that for you automatically. We will see how and when it will be done later. Now that the database has been created and the connection string information specified in the configuration file of the application, the next step is to modify the Startup.cs file and provide the necessary connection string information. The following code listing illustrates how the Startup.cs file looks after the configuration details have been specified. using Hangfire; using Microsoft.Owin; using Owin; using System; [assembly: OwinStartupAttribute(typeof(HangFire.Startup))] namespace HangFire { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); GlobalConfiguration.Configuration .UseSqlServerStorage("DefaultConnection"); BackgroundJob.Enqueue(() => Console.WriteLine("Getting Started with HangFire!")); app.UseHangfireDashboard(); app.UseHangfireServer(); } } } You’re all set. When you run the application and suffix the URL with “/hangfire”, you can see the Hangfire dashboard. When you execute this the very first time, a new table is created in the database. The tables that are created include AggregatedCounter, Counter, Hash, Job, JobParameter, JobQueue, List, Schema, Server, Set, and State. Creating a fire-and-forget background in Hangfire is quite simple. You can create a background job using the Enqueue() method of the BackgroundJob class. Here’s an example: BackgroundJob.Enqueue(() => Console.WriteLine("This is a fire-and-forget job that would run in the background.")); A delayed background job is one that waits (for the delay interval), then executes much the same way as a normal fire-and-forget background job. The following code snippet illustrates how you can create a delayed background job using the Schedule() method of the BackgroundJob class. BackgroundJob.Schedule(() => Console.WriteLine("This background job would execute after a delay."), TimeSpan.FromMilliseconds(1000)); If you were to execute jobs that would execute after a specific interval of time, you would need to create recurring jobs in Hangfire. To create a recurring job, you would have to leverage the RecurringJob class. Note that you can also specify “cron” expressions when scheduling jobs in Hangfire. The following code snippet illustrates how you can create a recurring job using the Hangfire library. RecurringJob.AddOrUpdate(() => Console.WriteLine("This job will execute once in every minute"), Cron.Minutely); Check out the Hangfire Highlighter tutorial for more information. Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos