using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Transport; using ZB.MOM.WW.ScadaBridge.TemplateEngine.Validation; using ZB.MOM.WW.ScadaBridge.Transport.Encryption; using ZB.MOM.WW.ScadaBridge.Transport.Export; using ZB.MOM.WW.ScadaBridge.Transport.Import; using ZB.MOM.WW.ScadaBridge.Transport.Serialization; namespace ZB.MOM.WW.ScadaBridge.Transport; public static class ServiceCollectionExtensions { public const string OptionsSection = "ScadaBridge:Transport"; /// /// Registers the Transport component services: encryptor, manifest builder/validator, serializers, resolver, exporter, importer, and session store. /// /// The service collection to register into. /// The same to allow chaining. public static IServiceCollection AddTransport(this IServiceCollection services) { ArgumentNullException.ThrowIfNull(services); services.AddOptions().BindConfiguration(OptionsSection); services.TryAddSingleton(TimeProvider.System); // Pipeline building blocks: stateless services live as singletons; the // resolver and exporter are scoped because they reach into per-request // repository scopes and the scoped DbContext. services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddScoped(); services.AddSingleton(); // T-004: per-IP-per-hour unlock rate limiter — design doc §11. Singleton // so the trailing-hour window is shared across every importer scope; the // counters live in-memory and reset on host restart (by design). services.AddSingleton(); // T-007: periodic eviction sweep so abandoned sessions clear without // needing a fresh Get() to trigger lazy eviction. services.AddHostedService(); // SemanticValidator is a stateless utility used by ApplyAsync; use // TryAdd so a host that already calls AddTemplateEngine() (which // registers the same type as Transient) wins. Either registration // satisfies the BundleImporter constructor. services.TryAddTransient(); services.AddScoped(); // Remaining concrete services added in later tasks. return services; } }