Creating Scheduled Tasks Using Hangfire in Asp.Net Core

Hangfire is a tool used to manage scheduled tasks in Asp.Net Core applications. Hangfire reliably executes scheduled tasks by creating a job process running behind the application.

To use Hangfire, you must first install the Hangfire package in your application. You can use NuGet Package Manager for this. Also, you need to choose a database to use Hangfire. Among the many database servers that Hangfire supports, we can count SQL Server, PostgreSQL, MySQL, Oracle, Redis and MongoDB.

It is an incredible solution for the tasks that we want to be done according to certain rules and at certain times in the background while the program is running on the server. It is a very useful auxiliary library with a web interface. With its dashboard, we can view your jobs historically and perform operations such as start-stop/restart.

First of all, let's create a database called HangFire in the local Db. We set the user login information of the database.

Now we are opening a web api project called TaskWithHangFire. We are adding the most up-to-date Hangfire package to our project via Nuget.

We add the connection string of the database we created to the application.json file.

We need to introduce Hangfire to the app. In this section, let's add Hangfire as a service in the ConfigureServices method in Startup.cs, and then we write code blocks that indicate that we will use this service in the Configure method.

In the above code block, we registered the hangfire database information to our application services. Afterwards, we stated that we will use Hangfire's server and dashboard services.

By default, the Hangfire dashboard is hosted at http://hangfire on the port where the application is running. Since we work in localhost, we can view the hangfire dashboard as follows when we run the application and go to http://localhost/hangfire via the browser.

Are the necessary tables created in the database when the application is started? Let’s check.

We have completed the Hangfire configuration, now if we look at the types of jobs we can create using the BackgroundJob class in the Hangfire library;

1- Fire-and-Forget Jobs

After the job is created, it runs and becomes a process. It won't work again. It is a disposable job.

2- Delayed Jobs

A job type that we can use for tasks that we want to run only once by setting a certain time information. It will run 4 minutes after the Job is registered as follows.

3- Recurring Jobs

Job type used for recurring, ie repetitive tasks. For example; When you need a job that you want to run every hour, we can define it as follows.

4- Continuation Jobs

It is a job type that we can use for jobs that have a parent-child relationship, that is, we wait for another job to complete for a job to work, and then we want it to work after it is finished.

For background work that you need to run in your application, you can quickly implement Hangfire and use it easily with its dashboard. You can do your transactions by using, which is the equivalent of Hangfire, or a queue solution.

I hope it was useful.