Hangfire – Background Processing in .NET and .NET Core Applications

https://www.hangfire.io/

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

See our blog

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!

{
"by": "bilekas",
"descendants": 0,
"id": 40247647,
"score": 4,
"time": 1714744167,
"title": "Hangfire – Background Processing in .NET and .NET Core Applications",
"type": "story",
"url": "https://www.hangfire.io/"
}
{
"author": null,
"date": "2024-12-09T00:00:00.000Z",
"description": "An easy way to perform fire-and-forget, delayed and recurring tasks in ASP.NET applications. No Windows Service required, backed by persistent storage.",
"image": "https://www.hangfire.io/img/twitter-crd.png",
"logo": "https://logo.clearbit.com/hangfire.io",
"publisher": "Hangfire",
"title": "Hangfire – Background Jobs for .NET and .NET Core",
"url": "https://www.hangfire.io/"
}
{
"url": "https://www.hangfire.io/",
"title": "Hangfire – Background Jobs for .NET and .NET Core",
"description": "Fire-and-Forget Jobs Fire-and-forget jobs are executed only once and almost immediately after creation. var jobId =...",
"links": [
"https://www.hangfire.io/"
],
"image": "https://www.hangfire.io/img/twitter-crd.png",
"content": "<div>\n <div>\n <div>\n <div>\n <div>\n <h3>Fire-and-Forget Jobs</h3>\n <p>\n Fire-and-forget jobs are executed <strong>only once</strong> and almost <strong>immediately</strong> after creation.\n </p>\n <pre><span>var</span> jobId = <span>BackgroundJob</span>.Enqueue(\n () =&gt; <span>Console</span>.WriteLine(<span>\"Fire-and-forget!\"</span>));</pre>\n </div>\n <div>\n <h3>Delayed Jobs</h3>\n <p>\n Delayed jobs are executed <strong>only once</strong> too, but not immediately, after a certain <strong>time interval</strong>.\n </p>\n <pre><span>var</span> jobId = <span>BackgroundJob</span>.Schedule(\n () =&gt; <span>Console</span>.WriteLine(<span>\"Delayed!\"</span>),\n <span>TimeSpan</span>.FromDays(7));</pre>\n </div>\n </div>\n <div>\n <div>\n <h3>Recurring Jobs</h3>\n <p>\n Recurring jobs fire <strong>many times</strong> on the specified <strong>CRON schedule</strong>.\n </p>\n <pre><span>RecurringJob</span>.AddOrUpdate(\n <span>\"myrecurringjob\"</span>,\n () =&gt; <span>Console</span>.WriteLine(<span>\"Recurring!\"</span>),\n <span>Cron</span>.Daily);</pre>\n </div>\n <div>\n <h3>Continuations</h3>\n <p>\n Continuations are executed when its parent job <strong>has been finished</strong>.\n </p>\n <pre><span>BackgroundJob</span>.ContinueJobWith(\n jobId,\n () =&gt; <span>Console</span>.WriteLine(<span>\"Continuation!\"</span>));</pre>\n </div>\n </div>\n <div>\n <div>\n <h3>Batches <a target=\"_blank\" href=\"https://www.hangfire.io/pro/\">Pro</a></h3>\n <p>Batch is a group of background jobs that is <strong>created atomically</strong> and considered as a single entity.</p>\n <pre><code><span>var</span> batchId = <span>BatchJob</span>.StartNew(x =&gt;\n{\n x.Enqueue(() =&gt; <span>Console</span>.WriteLine(<span>\"Job 1\"</span>));\n x.Enqueue(() =&gt; <span>Console</span>.WriteLine(<span>\"Job 2\"</span>));\n});</code></pre>\n </div>\n <div>\n <h3>Batch Continuations <a target=\"_blank\" href=\"https://www.hangfire.io/pro/\">Pro</a></h3>\n <p>\n Batch continuation is fired <strong>when all</strong> background jobs in a parent batch <strong>finished</strong>.\n </p>\n <pre><code><span>BatchJob</span>.ContinueBatchWith(batchId, x =&gt;\n{\n x.Enqueue(() =&gt; <span>Console</span>.WriteLine(<span>\"Last Job\"</span>));\n});</code></pre>\n </div>\n </div>\n </div>\n <div>\n <h3>Recent News</h3>\n<ul>\n <li>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/2024/12/09/hangfire.pro.redis-3.1.0.html\">\n <h5>Hangfire.Pro.Redis 3.1.0</h5>\n <p>\n Redis Sentinel support, a new transaction implementation that works with ElastiCache Serverless and faster reconnections on network problems.\n </p>\n <span>\n <i></i> December 9, 2024\n </span>\n </a>\n </li>\n <li>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/2024/11/27/hangfire-1.8.16.html\">\n <h5>Hangfire 1.8.16</h5>\n <p>\n 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.\n </p>\n <span>\n <i></i> November 27, 2024\n </span>\n </a>\n </li>\n <li>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/2024/10/23/hangfire-1.8.15.html\">\n <h5>Hangfire 1.8.15</h5>\n <p>\n 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.\n </p>\n <span>\n <i></i> October 23, 2024\n </span>\n </a>\n </li>\n <li>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/2024/10/16/hangfire.pro.redis-3.0.10.html\">\n <h5>Hangfire.Pro.Redis 3.0.10</h5>\n <p>\n Better transaction behavior in case of operation failures and fixes problems with distributed locks when `channelPrefix` option is used.\n </p>\n <span>\n <i></i> October 16, 2024\n </span>\n </a>\n </li>\n <li>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/2024/09/25/hangfire.pro.redis-3.0.9.html\">\n <h5>Hangfire.Pro.Redis 3.0.9</h5>\n <p>\n Better resiliency of subscription connections when listening empty queues or waiting for locks.\n </p>\n <span>\n <i></i> September 25, 2024\n </span>\n </a>\n </li>\n</ul>\n<p>\n <a target=\"_blank\" href=\"https://www.hangfire.io/blog/\">See our blog</a>\n</p>\n <h3>Courses</h3>\n </div>\n </div>\n <hr />\n <div>\n <div>\n <h3>Simple</h3>\n <p>\n <a href=\"https://docs.hangfire.io/en/latest/quick-start.html\" target=\"_blank\">Easy to set up, easy to use</a>. No Windows Service, no Windows Scheduler, no separate applications required.\n </p>\n <p>\n Background jobs are regular static or instance .NET methods with regular arguments – no base class or interface implementation required.\n </p>\n </div>\n <div>\n <h3>Persistent</h3>\n <p>\n Background jobs are created in a persistent storage – <a target=\"_blank\" href=\"https://docs.hangfire.io/en/latest/configuration/using-sql-server.html\">SQL Server</a> and <a target=\"_blank\" href=\"https://docs.hangfire.io/en/latest/configuration/using-redis.html\">Redis</a> supported officially, and a lot of other <a target=\"_blank\" href=\"https://www.hangfire.io/extensions.html#storages\">community-driven</a> storages.\n </p>\n <p>\n You can safely restart your application and use Hangfire with ASP.NET without worrying about application pool recycles.\n </p>\n </div>\n <div>\n <h3>Transparent</h3>\n <p>\n 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.\n </p>\n <p>\n Out of the box support for popular <a target=\"_blank\" href=\"https://docs.hangfire.io/en/latest/configuration/configuring-logging.html\">logging frameworks</a> allows you to catch errors early with zero configuration.\n </p>\n </div>\n </div>\n <div>\n <div>\n <h3>Reliable</h3>\n <p>\n Once a background job was created without any exception, Hangfire takes the responsibility to process it with the <em>at least once</em> semantics.\n </p>\n <p>\n You are free to throw unhandled exceptions or terminate your application – background jobs will be re-tried automatically.\n </p>\n </div>\n <div>\n <h3>Distributed</h3>\n <p>\n Background method calls and their arguments are serialized and may overcome the process boundaries.\n </p>\n <p>\n You can use Hangfire on different machines to get more processing power with no configuration – synchronization is performed automatically.\n </p>\n </div>\n <div>\n <h3>Extensible</h3>\n <p>\n Job filters allow you to add custom features to the background processing in a way similar to ASP.NET MVC action filters.\n </p>\n <p>\n Job storage access is fully abstracted and you can implement the support for your favorite storage. Dashboard supports modifications too.\n </p>\n </div>\n </div>\n <div>\n <div>\n <h3>Efficient</h3>\n <p>\n Although the default installation uses SQL Server and polling technique to fetch jobs, you can leverage <a target=\"_blank\" href=\"https://www.nuget.org/packages/Hangfire.SqlServer.MSMQ/\">MSMQ</a> or <a href=\"https://www.nuget.org/packages/Hangfire.Redis/\" target=\"_blank\">Redis</a> extensions to reduce the processing latency to minimum.\n </p> \n </div>\n <div>\n <h3>Self-maintainable</h3>\n <p>\n You don't need to perform manual storage clean-up – Hangfire keeps it as clean as possible and removes old records automatically. \n </p>\n </div>\n <div>\n <h3>Open Source</h3>\n <p>\n Hangfire is open source software and is completely free for commercial use. It is licensed under <a target=\"_blank\" href=\"https://github.com/HangfireIO/Hangfire/blob/main/LICENSE.md\">LGPLv3 license</a>.\n </p>\n <p>\n Fork <a href=\"https://github.com/HangfireIO/Hangfire\" target=\"_blank\">the project</a> and make contributions on GitHub!\n </p>\n </div>\n </div>\n</div>",
"author": "",
"favicon": "https://www.hangfire.io/favicon-32x32.png",
"source": "hangfire.io",
"published": "",
"ttr": 119,
"type": ""
}