using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace StellaOps.Provcache; /// /// Extension methods for registering Provcache services. /// public static partial class ProvcacheServiceCollectionExtensions { /// /// Adds Provcache services to the service collection. /// /// The service collection. /// The configuration section. /// The service collection for chaining. public static IServiceCollection AddProvcache( this IServiceCollection services, IConfiguration configuration) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configuration); var section = configuration.GetSection(ProvcacheOptions.SectionName); // Register options services.AddOptions() .Bind(section) .ValidateDataAnnotations() .ValidateOnStart(); services.AddOptions() .Bind(section.GetSection(LazyFetchHttpOptions.SectionName)) .ValidateDataAnnotations() .ValidateOnStart(); // Register core services services.AddSingleton(); // Register write-behind queue as hosted service services.AddSingleton(); services.AddHostedService(); services.AddHttpClient(HttpChunkFetcher.HttpClientName); services.AddProvcacheInvalidators(); return services; } /// /// Adds Provcache services with custom options. /// /// The service collection. /// Action to configure options. /// The service collection for chaining. public static IServiceCollection AddProvcache( this IServiceCollection services, Action configure) { ArgumentNullException.ThrowIfNull(services); ArgumentNullException.ThrowIfNull(configure); // Register options services.AddOptions() .Configure(configure) .ValidateDataAnnotations() .ValidateOnStart(); services.AddOptions() .ValidateDataAnnotations() .ValidateOnStart(); // Register core services services.AddSingleton(); // Register write-behind queue as hosted service services.AddSingleton(); services.AddHostedService(); services.AddHttpClient(HttpChunkFetcher.HttpClientName); services.AddProvcacheInvalidators(); return services; } }