A .NET 8 Worker Service that demonstrates Hangfire integration with Redis storage for distributed job processing.
- Hangfire Integration: Uses Hangfire with Redis storage for distributed job processing
- Worker Service: Background service that processes jobs without web interface
- Environment-based Configuration: Supports Development, Test, Staging, and Production environments
- Job Scheduling: Supports Fire-and-forget, Delayed, Recurring, and Queue-based jobs
- Structured Logging: Uses Serilog for comprehensive logging
- Windows Service Support: Can run as a Windows service
- Docker Support: Includes Docker and Docker Compose configuration
- .NET 8 SDK
- Redis Server (for distributed job storage)
- Docker (optional, for containerized deployment)
The application uses different Redis configurations based on the environment:
- Development:
localhost:6379, Database 0 - Test/Staging:
redis.local:6379, Database 0 - Production: Configurable Redis endpoint
Worker service configuration in appsettings.json:
{
"HangfireConfiguration": {
"WorkerCount": 20,
"EnableMetrics": true,
"EnableHeartbeat": true,
"HeartbeatInterval": "00:00:30"
}
}-
Start Redis Server:
# Using Docker docker run -d -p 6379:6379 redis:7-alpine # Or install Redis locally
-
Run the Worker Service:
dotnet run --environment Development
-
Monitor Jobs:
- Jobs are processed in the background
- Check application logs for job execution details
- Use Redis CLI to inspect job storage:
redis-cli
-
Start the entire stack:
docker-compose up -d
-
Monitor the service:
- Check logs:
docker-compose logs -f hangfire-worker - Monitor Redis:
docker-compose exec redis redis-cli
- Check logs:
-
Publish the application:
dotnet publish -c Release -o ./publish
-
Install as Windows Service:
sc create "Hangfire Service" binPath="C:\path\to\publish\Hangfire.Poc.exe" sc start "Hangfire Service"
The worker service automatically processes jobs from these queues:
- critical - High priority jobs
- default - Standard priority jobs
- normal - Normal priority jobs
- low - Low priority jobs
├── Configuration/ # Configuration models
├── Extensions/ # Service extensions and Hangfire setup
├── Services/ # Background services and job processors
├── Program.cs # Application entry point
├── appsettings.*.json # Environment-specific configurations
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose for development
└── README.md # This file
- HangfireServiceExtensions: Configures Hangfire services and Redis storage
- HangfireWorkerService: Background service for job processing and scheduling
- JobService: Sample job implementations with different queue priorities
- Configuration Classes: Strongly-typed configuration models
The worker service includes sample jobs that demonstrate different Hangfire features:
- Fire-and-forget jobs: Executed once, immediately
- Delayed jobs: Executed once, after a specified delay
- Recurring jobs: Executed repeatedly based on CRON expressions
- Queued jobs: Executed in specific queues (critical, default, normal, low)
Key environment variables for configuration:
DOTNET_ENVIRONMENT: Sets the environment (Development, Test, Staging, Production)ASPNETCORE_ENVIRONMENT: Alternative environment variable
The application uses Serilog for structured logging:
- Console output for development
- File logging in
logs/directory - Configurable log levels per environment
- Job Execution: Monitor through application logs
- Redis Storage: Use Redis CLI or management tools to inspect job storage
- Performance: Built-in Hangfire metrics and heartbeat monitoring
-
Redis Connection Issues:
- Ensure Redis server is running and accessible
- Check Redis configuration in appsettings files
- Verify network connectivity and firewall settings
-
Authentication Issues:
- Verify credentials in appsettings files
- Check browser developer tools for authentication errors
-
Service Issues:
- Check application logs in
logs/directory - Verify environment variables are set correctly
- Ensure all required packages are installed
- Check application logs in