Setting up Hangfire with .NET in 5 minutes

Setting up Hangfire with .NET in 5 minutes

Have you ever needed to schedule background tasks in your .NET applications? Hangfire is a powerful library that allows you to easily schedule and manage tasks. In this article, I'll show you how to set up Hangfire in just 5 minutes.

Step 1: Install Hangfire

The first step is to add Hangfire to your project. You can do this via NuGet. Open the Package Manager Console and run the following command:

Install-Package Hangfire        

Step 2: Configure Hangfire in Startup.cs

With Hangfire installed, let's configure it in the 'Startup.cs' file. First, add the necessary references:

using Hangfire;
using Hangfire.MemoryStorage;        

Next, configure Hangfire in the 'ConfigureServices' method:

public void ConfigureServices(IServiceCollection services)
{
    // Configuring Hangfire to use in-memory storage
    services.AddHangfire(config => config.UseMemoryStorage());

    // Adding the Hangfire server
    services.AddHangfireServer();

    // Other services
    services.AddControllers();
}        

Finally, add the Hangfire middleware in the 'Configure' method:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

    // Adding the Hangfire Dashboard
    app.UseHangfireDashboard();
}        

Step 3: Create and Schedule a Job

Now that Hangfire is configured, let's create and schedule a simple job. Create a new class called 'BackgroundJobService':

public class BackgroundJobService
{
    public void Execute()
    {
        // Your task code
        Console.WriteLine("Task executed successfully!");
    }
}        

To schedule this job, add the following code to Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Previous code...

    // Scheduling a task to run every minute
    RecurringJob.AddOrUpdate<BackgroundJobService>(job => job.Execute(), Cron.Minutely);

    // Adding the Hangfire Dashboard
    app.UseHangfireDashboard();
}        

Step 4: Run the Application

With everything configured, you can now run the application. Navigate to /hangfire in your browser to access the Hangfire Dashboard and see the scheduled jobs.

Final Thoughts

Hangfire makes it easy to schedule and manage background tasks in .NET applications. With just a few lines of code, you can configure, schedule, and monitor tasks efficiently. Explore more Hangfire features such as priority queues, recurring jobs, and integration with different storage providers to make the most of this powerful tool.

Share this article if you found it helpful and comment below if you have any questions or suggestions!

Gerald Hamilton Wicks

Full Stack Engineer | React | Node | JavaScript | Typescript | Next | MERN Developer

8 个月

Great guide on setting up Hangfire, Pedro Constantino! Your step-by-step instructions make integrating background tasks into .NET applications seamless. Looking forward to exploring more features like recurring jobs and task monitoring. Thanks for simplifying this process! ???

Marcos Schead

Software Developer | Full Stack Engineer | Javascript | NodeJS | ReactJS | Typescript | AWS

8 个月

Pedro Constantino Indeed, it's a fantastic tool! Last year, I used Hangfire on a C# project, and it was a pleasure to work with. ??

Fábio Salom?o

Software Engineer | Full Stack Developer | C# | .Net | React | Blazor | Typescript | Docker | Azure | Azure Devops | GitHub | API LLM

8 个月

Great guide! Hangfire is indeed a powerful tool for scheduling background tasks in .NET. I loved the simplicity and speed of the tutorial. It will be very helpful for those who need to implement this functionality. Thanks for sharing!

Guilherme Lauxen Persici

Cloud Software Engineer | Fullstack Software Engineer | AWS | PHP | Laravel | ReactJs | Docker

8 个月

Thanks for sharing Pedro Constantino

Lucas Wolff

.NET Developer | C# | TDD | Angular | Azure | SQL

8 个月

Nice explanation!

要查看或添加评论,请登录

Pedro Constantino的更多文章

社区洞察

其他会员也浏览了