
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.
Begin by creating a new ASP.NET Core or .NET Core Console Application. This will serve as the foundation for implementing Hangfire.
Install Hangfire along with the Hangfire.MemoryStorage package via NuGet:
Install-Package HangfireInstall-Package Hangfire.MemoryStorage
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.
public void ConfigureServices(IServiceCollection services){services.AddHangfire(config => config.UseMemoryStorage());services.AddHangfireServer();}
Optionally, integrate the Hangfire dashboard for job monitoring:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){app.UseHangfireDashboard();}
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}}
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 configurationsRecurringJob.AddOrUpdate<IMyBackgroundJob>(job => job.DoWorkAsync(),Cron.Daily);}
Lastly, ensure to write unit tests for your job and use the Hangfire Dashboard to monitor its execution.
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.
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
If you found this guide beneficial, don’t hesitate to share it with your network.
Until the next guide, happy coding!
Quick Links
Legal Stuff