It seems like every application I’ve ever written, at some point needs to run a task in the background that is to be scheduled at some point in the future. My need for tasks as reoccurring batch jobs has decreased significantly over the years because of my applications being more event driven. Nowadays, I will often use Event Stores Persistent Subscriptions with my event stream as a queue. However, I’m not event sourcing all parts of the application and not always using Event Store. If you have any questions, please follow me on Twitter.
Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
HangfireI stumbled upon Hangfire a couple years ago when trying to find solution to running background tasks in a .NET console application running as a service with Topshelf. When I was trying to find a solution, I need to have tasks distributed across multiple worker services. Hangfire has this problem solved.
An easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications. No Windows Service required. Backed by persistent storages. Open and free for commercial use. Background method calls and their arguments are serialized and may overcome the process boundaries. You can use Hangfire on different machines to get more processing power with no configuration – synchronization is performed automatically.
Getting StartedThe docs are really good over at the Hangfire site, so I don’t want to rewrite what is already over there. I do however just want to give you enough to show you how simple it is to use and let you decide if its a viable solution for you. There are two ways in which you can get started. If you plan on using Hangfire within an ASP.NET Web Application and want to use SQL Server as your storage mechanism, then you can install the boostrapper package which has everything you need.
Install-Package HangFire -Version 1.5.6If you plan on using in any other project type (Windows Service, Console, OWIN-Compatible Web Application, Azure Worker Role) or different storage mechanism, you can install the Hangfire.Core.
Install-Package HangFire.Core -Version 1.5.6
ConfigurationConfiguration is the same if you are in an ASP.NET Web Application or any other project type. What differs is where you put this configuration. But it is incredibly straight forward. You simply use the GlobalConfiguration class to configure the entry point.
ServerThe server is what processes the background tasks we will define later. In order to create a server, you simply create a BackgroundJobServer. This can be called from any project you wish to act as a Hangfire server. As with configuration, where you place create the BackgroundJobServerwill depend on the project type.
Create Background TaskThere are different types of background tasks that we can now create from any other project type. These calls do not need to be made from the same application as the server.
Fire-and-forgetYou simply call the Enqueue with your action you want to invoke on the server.
TheFor my example, I’m going to Enqueue a new task in the same application as my server. When you run this application, here is the console output.
Enqueuemethod does not call the target method immediately, it runs the following steps instead:
- Serialize a method information and all its arguments.
- Create a new background job based on the serialized information.
- Save background job to a persistent storage.
- Enqueue background job to its queue.