Pro EP 14ย : Hangfire inย .NET

Muhammad Waseem
Becomeย .NET Proย !
2 min readMar 1, 2023

๐Ÿ’ก ๐‡๐š๐ง๐ ๐Ÿ๐ข๐ซ๐ž ๐ข๐ง.๐๐„๐“
Hang fire is open-source and used to schedule jobs with a simple implementation in .NET Core , it can create process and manage our background jobs with available methods in it. It is reliable and persistent along with monitoring facility.

๐ŸŽฏ ๐‡๐จ๐ฐ ๐ญ๐จ ๐”๐ฌ๐ž ๐ข๐ญ
1. Install Hang fire via Nuget Package
2. Add its configuration in Program.cs
3. Use it

๐ŸŽฏ ๐‚๐จ๐ง๐Ÿ๐ข๐ ๐ฎ๐ซ๐ž๐ ๐ฐ๐ข๐ญ๐ก ๐’๐๐‹
We need to provide a SQL database connection string in Program.cs for its working but we donโ€™t need to create tables it will take care of it on its own.

๐ŸŽฏ ๐๐ž๐ง๐ž๐Ÿ๐ข๐ญ๐ฌ
As a developer we often come up with situations where we need to send the mass notifications/emails/reports/Events so we can utilize it. It supports CRON expression and we can schedule events of all kind minutely/daily/weekly/monthly/yearly. Apart from that we have options to schedule events on the basis of some events as well

Suppose, If you want that on the completion of some ABC job I want to run a XYZ then we can do it with Hangfire method ContinueJobWith as shown in second example of code snippets.

Letโ€™s add it in Program.cs

Add this piece of code for scheduling

After that you will see that tables have been created in your database

Using SQL Query we can check our scheduled jobs

If you want to help the author in growing

  1. Subscribe my Weekly .NET Newsletter of C#/.NET
  2. Follow me on LinkedIn/Twitter OR Clap at least 50 times.
  3. Get exclusive .NET Questions & Answers by becoming a Patron , I add almost 25+ new questions with explanation per month.
  4. Download my eBook at Gum road that contains 30+ .NET Tips.

--

--