HomeContact

Efficient Task Scheduling in .NET Applications with Hangfire and In-Memory Storage

By Shady Nagy
Published in dotnet
January 01, 2024
1 min read
Efficient Task Scheduling in .NET Applications with Hangfire and In-Memory Storage

Table Of Contents

01
Introduction
02
Step 1: Create Your Project
03
Step 2: Install Necessary Packages
04
Step 3: Configure Hangfire in Your Application
05
Step 4: Define and Implement Your Background Job
06
Step 5: Schedule Your Job
07
Step 6: Testing and Monitoring
08
Conclusion
09
Feedback and Questions

Introduction

In modern .NET applications, efficiently managing background tasks like data processing, batch updates, or periodic cleanups is crucial. However, developers often face challenges in scheduling these tasks effectively without impacting the application’s performance. This is where Hangfire steps in - a powerful library for handling background jobs in .NET applications.

One common hurdle is the dependency on persistent storage like SQL Server for job scheduling, which may not be ideal for all scenarios, especially during development or when dealing with transient data. To address this, we can leverage Hangfire with in-memory storage, offering a lightweight, yet robust solution for task scheduling.

In this post, we’ll explore how to set up Hangfire with in-memory storage in a .NET application. This setup is perfect for scenarios where you need a simple, in-process job store without the overhead of a database.

Step 1: Create Your Project

Begin by creating a new ASP.NET Core or .NET Core Console Application. This will serve as the foundation for implementing Hangfire.

Step 2: Install Necessary Packages

Install Hangfire along with the Hangfire.MemoryStorage package via NuGet:

Install-Package Hangfire
Install-Package Hangfire.MemoryStorage

Step 3: Configure Hangfire in Your Application

In your Startup.cs, set up Hangfire to use in-memory storage. This configuration eliminates the need for a persistent database and is ideal for lightweight applications or development environments.

ConfigureServices Method:
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(config => config.UseMemoryStorage());
services.AddHangfireServer();
}
Configure Method:

Optionally, integrate the Hangfire dashboard for job monitoring:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHangfireDashboard();
}

Step 4: Define and Implement Your Background Job

Create a job interface and its implementation. Adhering to SOLID principles ensures your application remains scalable and maintainable.

public interface IMyBackgroundJob
{
Task DoWorkAsync();
}
public class MyBackgroundJob : IMyBackgroundJob
{
public async Task DoWorkAsync()
{
// Task logic here
}
}

Step 5: Schedule Your Job

In the Configure method, schedule your job using RecurringJob.AddOrUpdate. This method ensures your job runs according to a specified schedule.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ... Other configurations
RecurringJob.AddOrUpdate<IMyBackgroundJob>(
job => job.DoWorkAsync(),
Cron.Daily);
}

Step 6: Testing and Monitoring

Lastly, ensure to write unit tests for your job and use the Hangfire Dashboard to monitor its execution.

Conclusion

Integrating Hangfire with in-memory storage in your .NET application simplifies the process of managing background tasks. This approach is not only efficient but also aligns with clean architecture and OOP principles. Whether you’re working on a large-scale application or a simple project, Hangfire offers a flexible and powerful solution for task scheduling.

Feedback and Questions

Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:

  1. Email: shady@shadynagy.com
  2. Twitter: @ShadyNagy_
  3. LinkedIn: Shady Nagy
  4. GitHub: ShadyNagy

If you found this guide beneficial, don’t hesitate to share it with your network.

Until the next guide, happy coding!


Tags

#.NETCore#Hangfire#InMemoryStorage#TaskScheduling#CSharp

Share


Previous Article
Unraveling Performance Bottlenecks in .NET Applications A Deep Dive with Code Examples
Shady Nagy

Shady Nagy

Software Innovation Architect

Topics

AI
Angular
dotnet
GatsbyJS
Github
Linux
MS SQL
Oracle

Related Posts

Automate .NET Package Deployment to NuGet with GitHub Actions
Automate .NET Package Deployment to NuGet with GitHub Actions
October 01, 2024
1 min

Quick Links

Contact Us

Social Media