Hangfire – Background Processing in .NET and .NET Core Applications
Fire-and-Forget Jobs
Fire-and-forget jobs are executed only once and almost immediately after creation.
var jobId = BackgroundJob.Enqueue( () => Console.WriteLine("Fire-and-forget!"));
Delayed Jobs
Delayed jobs are executed only once too, but not immediately, after a certain time interval.
var jobId = BackgroundJob.Schedule( () => Console.WriteLine("Delayed!"), TimeSpan.FromDays(7));
Recurring Jobs
Recurring jobs fire many times on the specified CRON schedule.
RecurringJob.AddOrUpdate( "myrecurringjob", () => Console.WriteLine("Recurring!"), Cron.Daily);
Continuations
Continuations are executed when its parent job has been finished.
BackgroundJob.ContinueJobWith( jobId, () => Console.WriteLine("Continuation!"));
Batches Pro
Batch is a group of background jobs that is created atomically and considered as a single entity.
var batchId = BatchJob.StartNew(x =>
{
x.Enqueue(() => Console.WriteLine("Job 1"));
x.Enqueue(() => Console.WriteLine("Job 2"));
});
Batch Continuations Pro
Batch continuation is fired when all background jobs in a parent batch finished.
BatchJob.ContinueBatchWith(batchId, x =>
{
x.Enqueue(() => Console.WriteLine("Last Job"));
});
Recent News
-
Hangfire.Pro.Redis 3.1.0
Redis Sentinel support, a new transaction implementation that works with ElastiCache Serverless and faster reconnections on network problems.
December 9, 2024 -
Hangfire 1.8.16
Fixed regressions from 1.8.15, safe defaults for the Newtonsoft.Json package when previous versions are used and project-related tunings to speed up the build pipeline.
November 27, 2024 -
Hangfire 1.8.15
New AutomaticRetryAttribute.ExceptOn property to skip retries for specific exceptions, improved loopback address detection, fixed localization-related issues, fixed build failure with the `build.bat` command on first restore and decreased pressure on Garbage Collector.
October 23, 2024 -
Hangfire.Pro.Redis 3.0.10
Better transaction behavior in case of operation failures and fixes problems with distributed locks when `channelPrefix` option is used.
October 16, 2024 -
Hangfire.Pro.Redis 3.0.9
Better resiliency of subscription connections when listening empty queues or waiting for locks.
September 25, 2024
Courses
Simple
Easy to set up, easy to use. No Windows Service, no Windows Scheduler, no separate applications required.
Background jobs are regular static or instance .NET methods with regular arguments – no base class or interface implementation required.
Persistent
Background jobs are created in a persistent storage – SQL Server and Redis supported officially, and a lot of other community-driven storages.
You can safely restart your application and use Hangfire with ASP.NET without worrying about application pool recycles.
Transparent
Built-in web interface allow you to see the whole picture of your background processing, as well as observe the state of each background job.
Out of the box support for popular logging frameworks allows you to catch errors early with zero configuration.
Reliable
Once a background job was created without any exception, Hangfire takes the responsibility to process it with the at least once semantics.
You are free to throw unhandled exceptions or terminate your application – background jobs will be re-tried automatically.
Distributed
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.
Extensible
Job filters allow you to add custom features to the background processing in a way similar to ASP.NET MVC action filters.
Job storage access is fully abstracted and you can implement the support for your favorite storage. Dashboard supports modifications too.
Efficient
Although the default installation uses SQL Server and polling technique to fetch jobs, you can leverage MSMQ or Redis extensions to reduce the processing latency to minimum.
Self-maintainable
You don't need to perform manual storage clean-up – Hangfire keeps it as clean as possible and removes old records automatically.
Open Source
Hangfire is open source software and is completely free for commercial use. It is licensed under LGPLv3 license.
Fork the project and make contributions on GitHub!