DEV Community

Pradeep Pradyumna
Pradeep Pradyumna

Posted on

Build your first Hangfire job .NET8 with PostgreSQL

In this article, I will share a few simple steps you will need to create your first hangfire job using .NET 8(the latest at the time of writing this article) with Postgres as the database.

Also, please be aware that in this article I'm not going to brief what Hangfire is, as there are plenty of articles already written by wise authors. :)

Alright, let's get started.

1. First step first

We can create either an ASP.NET Core Web App or API (it doesn't really matter what you choose). However, for simplicity, I'm using an API project targeting.NET8

2. Install Nuget Packages

You'll need to install the following Nuget packages
Npgsql.EntityFrameworkCore.PostgreSQL
Microsoft.EntityFrameworkCore.Design
Hangfire.AspNetCore
Hangfire.PostgreSql

That's all you need!

3. Create a DBContext

You will need a custom DBContext class to run a DB Migration

    public class DefaultDbContext : DbContext
    {
        public DefaultDbContext(DbContextOptions<DefaultDbContext> options)
            : base(options) { }
    }
Enter fullscreen mode Exit fullscreen mode

4. Create a DB in Postgres

Just go ahead and create a database called HangfireSample
This is all you need to do at the DB level!

5. Configuration

Now before you run migration configure the DB path in appsettings.json and update program.cs

"ConnectionStrings": {
  "defaultConnection": "Host=localhost;Port=5432;Username=postgres;Password=YOUR_PWD;Database=HangfireSample"
}
Enter fullscreen mode Exit fullscreen mode

Update the below code in program.cs

builder.Services.AddEntityFrameworkNpgsql().AddDbContext<DefaultDbContext>(options => {
    options.UseNpgsql(builder.Configuration.GetConnectionString("defaultConnection"));
});
Enter fullscreen mode Exit fullscreen mode

6. Run migration

Just open Package Manager Console and run the below commands

dotnet ef migrations add InitContext
dotnet ef database update
Enter fullscreen mode Exit fullscreen mode

This will create __EFMigrationsHistory under public schema

7. Almost done

Now configure Hangfire service in program.cs with the below code

builder.Services.AddHangfire(x =>
    x.UsePostgreSqlStorage(builder.Configuration.GetConnectionString("defaultConnection")));
Enter fullscreen mode Exit fullscreen mode

And update middleware

app.UseHangfireDashboard("/dashboard");
app.UseHangfireServer();

Enter fullscreen mode Exit fullscreen mode

This was all you had to do create the hangfire server and storage.

Now, let's create a simple job.

8. First Hangfire job

Just copy the code below

BackgroundJob.Enqueue(() => Console.WriteLine("My first handfire job!"));

Enter fullscreen mode Exit fullscreen mode

Now, you run the solution!

To see your job, visit
https://localhost:44397/dashboard

And if you go to https://localhost:44397/dashboard/jobs/succeeded
you'll see the job you just executed.

Also, just you know, if you check the HangfireSample database, you'll the hangfire tables would be created under hangfire schema.

Just for your reference here is the complete working sample of the code I just explained.

I hope it helped you!

Top comments (0)