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

Table Of Contents
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 HangfireInstall-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 configurationsRecurringJob.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:
- Email: shady@shadynagy.com
- Twitter: @ShadyNagy_
- LinkedIn: Shady Nagy
- GitHub: ShadyNagy
If you found this guide beneficial, don’t hesitate to share it with your network.
Until the next guide, happy coding!







