Background Services
Documentation in Progress
This documentation is currently being developed. The session management and dependency injection infrastructure for background services is implemented.
Status: ✅ Core functionality complete, 📝 Documentation in progress
Expected completion: After Issue 002-106 (Cross-Component Integration Layer)
Coming Soon
This page will provide guidance for running long-term device operations as background services, including:
- Windows Services: Creating Windows Services for device monitoring
- ASP.NET Core Background Services: Hosted service implementation patterns
- Device Connection Pooling: Managing multiple persistent connections
- Data Collection Services: Continuous sensor data logging
- Watchdog Services: Monitoring device health and automatic recovery
- Scheduled Tasks: Periodic device maintenance and data collection
- Resource Management: Proper disposal and memory management
- Error Recovery: Robust background service error handling
Quick Preview
csharp
// Example of what's coming - background device monitoring service
public class DeviceMonitoringService : BackgroundService
{
private readonly IDeviceFactory _deviceFactory;
private readonly ILogger<DeviceMonitoringService> _logger;
private readonly BelayConfiguration _config;
public DeviceMonitoringService(
IDeviceFactory deviceFactory,
ILogger<DeviceMonitoringService> logger,
IOptions<BelayConfiguration> config)
{
_deviceFactory = deviceFactory;
_logger = logger;
_config = config.Value;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using var device = _deviceFactory.CreateSerialDevice("COM3");
while (!stoppingToken.IsCancellationRequested)
{
try
{
if (device.State != DeviceConnectionState.Connected)
{
await device.ConnectAsync(stoppingToken);
}
var temperature = await device.ExecuteAsync<float>("sensor.read_temp()");
var humidity = await device.ExecuteAsync<float>("sensor.read_humidity()");
_logger.LogInformation("Sensor data: {Temperature}°C, {Humidity}%",
temperature, humidity);
// Store data, trigger alerts, etc.
await Task.Delay(_config.MonitoringIntervalMs, stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in device monitoring service");
await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken);
}
}
}
}
// Program.cs registration
builder.Services.AddBelay();
builder.Services.AddHostedService<DeviceMonitoringService>();
Related Documentation
- Session Management - Long-term connection management
- Health Monitoring - Service health checks and monitoring
- Configuration - Service configuration and settings
- Error Handling - Background service error recovery
Need help now? Check our GitHub Discussions or review the session management guide.