using Microsoft.Extensions.Options; namespace ECMJobRunner.WebCron.ProfileWorker; public static class DependencyInjection { public static IServiceCollection AddProfileWorker(this IServiceCollection services, IConfiguration configuration) { // Configure ProfileWorker options from appsettings.json services.Configure( configuration.GetSection(ProfileWorkerOptions.SectionName)); // Validate options at startup services.AddSingleton, ProfileWorkerOptionsValidator>(); services.AddHostedService(); services.AddSingleton(); services.AddScoped(); return services; } } /// /// Validates ProfileWorkerOptions configuration at startup. /// internal class ProfileWorkerOptionsValidator : IValidateOptions { public ValidateOptionsResult Validate(string? name, ProfileWorkerOptions options) { if (options.IntervalMS <= 0) { return ValidateOptionsResult.Fail("ProfileWorker:IntervalMs must be greater than 0"); } if (options.IntervalMS < 100) { return ValidateOptionsResult.Fail("ProfileWorker:IntervalMs should be at least 100ms to avoid excessive CPU usage"); } return ValidateOptionsResult.Success; } }