Class ServiceCollectionExtensions
Namespace: Belay.Extensions
Assembly: Belay.Extensions.dll
Extension methods for registering Belay.NET services with dependency injection.
public static class ServiceCollectionExtensions
Inheritance
object ← ServiceCollectionExtensions
Inherited Members
object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.MemberwiseClone(), object.ReferenceEquals(object?, object?), object.ToString()
Examples
Basic Registration
// Program.cs or Startup.cs
public void ConfigureServices(IServiceCollection services)
{
// Simple registration with defaults
services.AddBelay();
// With custom configuration
services.AddBelay(options => {
options.DefaultTimeoutMs = 10000;
options.EnableCaching = true;
options.MaxConcurrentOperations = 4;
});
}
Configuration-based Registration
// appsettings.json
{
"Belay": {
"DefaultTimeoutMs": 15000,
"EnableCaching": true,
"DeviceDefaults": {
"SerialBaudRate": 115200,
"ConnectionRetries": 3
}
}
}
// Program.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddBelay(Configuration, "Belay");
}
Full Enterprise Setup with Health Checks
public void ConfigureServices(IServiceCollection services)
{
// Register all Belay services
services.AddBelay(options => {
options.DefaultTimeoutMs = 30000;
options.EnableCaching = true;
});
// Add health checks for device monitoring
services.AddBelayHealthChecks(healthOptions => {
healthOptions.SystemHealthCheckTimeoutSeconds = 10;
healthOptions.AddDeviceCheck("primary-sensor", "COM3", timeoutSeconds: 5);
healthOptions.AddDeviceCheck("backup-sensor", "COM4", timeoutSeconds: 5);
healthOptions.AddDeviceCheck("test-subprocess", "micropython", timeoutSeconds: 15);
});
// Add custom executors
services.AddBelayExecutors();
}
// Usage in controllers/services
public class SensorController : ControllerBase
{
private readonly IDeviceFactory deviceFactory;
public SensorController(IDeviceFactory deviceFactory)
{
this.deviceFactory = deviceFactory;
}
[HttpGet("temperature")]
public async Task<ActionResult<float>> GetTemperature()
{
using var device = this.deviceFactory.CreateSerialDevice("COM3");
await device.ConnectAsync();
float temp = await device.ExecuteAsync<float>(@"
import machine
sensor = machine.ADC(machine.Pin(26))
reading = sensor.read_u16()
(reading * 3.3 / 65535) * 100
");
return Ok(temp);
}
}
Background Service Integration
public class DeviceMonitoringService : BackgroundService
{
private readonly IServiceScopeFactory scopeFactory;
private readonly ILogger<DeviceMonitoringService> logger;
public DeviceMonitoringService(IServiceScopeFactory scopeFactory,
ILogger<DeviceMonitoringService> logger)
{
this.scopeFactory = scopeFactory;
this.logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
using var scope = this.scopeFactory.CreateScope();
var deviceFactory = scope.ServiceProvider.GetBelayDeviceFactory();
try
{
using var device = deviceFactory.CreateSerialDevice("COM3");
await device.ConnectAsync(stoppingToken);
var reading = await device.ExecuteAsync<float>("read_sensors()", stoppingToken);
this.logger.LogInformation("Sensor reading: {Reading}", reading);
}
catch (Exception ex)
{
this.logger.LogError(ex, "Failed to read sensors");
}
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
}
}
}
// Register background service
services.AddHostedService<DeviceMonitoringService>();
Remarks
These extensions provide multiple registration patterns for Belay.NET services, supporting both simple registration and comprehensive enterprise configurations with health checks, custom factories, and configuration binding.
Methods
Adds all Belay.NET services to the service collection with default configuration.
AddBelay(IServiceCollection, Action<BelayConfiguration>)
Adds all Belay.NET services to the service collection with configuration.
AddBelay(IServiceCollection, IConfiguration, string)
Adds all Belay.NET services to the service collection with IConfiguration binding.
AddBelayCore(IServiceCollection)
Adds Belay.NET core services to the service collection.
AddBelayExecutors(IServiceCollection)
Adds Belay.NET executor services as singletons. Note: In the simplified architecture, executors are accessed directly from Device instances rather than being created through dependency injection.
AddBelayFactories(IServiceCollection)
Adds Belay.NET factory services to the service collection.
AddBelayHealthChecks(IServiceCollection, Action<BelayHealthCheckOptions>?)
Adds Belay.NET health checks to the service collection.
GetBelayDeviceFactory(IServiceProvider)
Creates a service scope and resolves a device factory.
GetBelayExecutorFactory(IServiceProvider)
Creates a service scope and resolves an executor factory.