Read the full article at :
https://coderlegion.com/7559/tutorial-building-a-net-9-console-app-with-hangfire-and-channels
https://dev.to/stevsharp/tutorial-building-a-net-9-console-app-with-hangfire-and-channels-1g02
CustomerSyncConsole is a .NET 8 console application that demonstrates:
- Polling simulated data
- Using Channels for message passing
- Triggering Hangfire background jobs when conditions are met
- In-memory storage for simplicity
The application uses System.Threading.Channels to implement a producer-consumer pattern:
DataPollingServiceacts as the producer, writing work items into the channel.ChannelConsumerServiceacts as the consumer, reading items and processing them.
Channels provide thread-safe, high-performance communication between background tasks without manual locking or complex queue management.
The app uses IHostedService implementations (BackgroundService) to run long-running tasks:
DataPollingServiceruns periodically to fetch or simulate data.ChannelConsumerServicecontinuously listens for new items in the channel.
Hosted services integrate seamlessly with the .NET Generic Host, allowing dependency injection, logging, and graceful shutdown.
- .NET 9 SDK
- NuGet packages:
- Hangfire
- Hangfire.MemoryStorage
- Microsoft.Extensions.Hosting
- Microsoft.Extensions.Logging.Console
dotnet restore
dotnet runDataPollingServicesimulates fetching data and writes work items to a channel.ChannelConsumerServicereads from the channel and enqueues Hangfire jobs for CustomerId = 1.- Hangfire processes jobs in-memory.