An open-source library called Hangfire is used in.NET applications to process jobs in the background. It enables you to carry out lengthy operations, such processing photos, sending emails, or any other work, asynchronously without interfering with the main request thread. Hangfire manages job persistence, retries, and scheduling to maintain the responsiveness of your application while guaranteeing the reliable execution of crucial background operations.
Hangfire supports several types of jobs.
- Fire-and-Forget Jobs: Executed only once immediately after creation.
- Delayed Jobs: Scheduled to run after a specified delay.
- Recurring Jobs: Run according to a defined schedule (e.g., every day at a particular time).
- Continuation Jobs: Triggered when their parent job completes.
Prerequisites
Before you begin, ensure you have,
- .NET 8 SDK installed.
- A code editor (Visual Studio or VS Code).
- SQL Server or LocalDB for job storage.
- Basic knowledge of C# and ASP.NET Core.
1. Create a New Minimal API Project
Open your terminal.
2. Add Hangfire NuGet Packages
Navigate to your project folder and run.
3. Configure the Database
Update appsettings.json with your connection string. For example, using LocalDB.
Hangfire will automatically create the necessary tables.
Replace the contents of Program.cs with the following code.
Hangfire Configuration
- AddHangfire registers Hangfire with SQL Server storage using the provided connection string.
- AddHangfireServer starts a background server to process the queued jobs.
Endpoints
- Root ("/"): Returns a basic welcome message.
- Enqueue ("/enqueue"): Uses BackgroundJob.Enqueue to immediately queue a fire-and-forget job.
- Schedule ("/schedule"): Uses BackgroundJob.Schedule to queue a job that will execute after a 1-minute delay.
- Hangfire Dashboard: Accessible at /hangfire, it provides real-time monitoring of job statuses (enqueued, processing, succeeded, or failed).
Architecture Diagram
Step-by-Step Flow
- Client Interaction: A client calls /enqueues, or /schedules to add jobs.
- Job Storage: Hangfire persists the job details in SQL Server.
- Job Processing: The Hangfire server continuously polls the queue and executes ready jobs.
- Dashboard Monitoring: The Hangfire Dashboard lets you view job states and monitor execution.
Key Terms Explained
- Fire-and-Forget Job (Enqueue): Enqueued tasks that run as soon as they’re picked up. Ideal for operations that don't require an immediate response.
- Delayed Job (Schedule): Tasks that start after a delay—useful for follow-up operations like sending reminders.
- Background Job: Any task executed asynchronously outside the main HTTP request/response cycle.
- Hangfire Server: A service that retrieves and executes background jobs from storage.
- Hangfire Dashboard: A web-based UI for monitoring job statuses. For production, secure this dashboard with proper authentication.
Running and Testing
Start the Application
Test the Endpoints
- Root: Navigate to http://localhost:5000/ to see "Hello World from .NET 8 and Hangfire!"
- Enqueue: Visit http://localhost:5000/enqueue to dispatch a fire-and-forget job.
- Schedule: Visit http://localhost:5000/schedule to schedule a job for execution after one minute.
- Dashboard: Open http://localhost:5000/hangfire to monitor jobs in real time.
Output Samples
Then, to enhance your background processing capabilities, think about investigating repeating jobs, job continuations, or including bespoke error handling.
Have fun with your coding!
0 comments:
Post a Comment