📄
ServiceCollectionExtensions.cs
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using MSearch.Domain; namespace MSearch.SearchProviders.StackExchange; public static class ServiceCollectionExtensions { public static IServiceCollection AddStackExchangeSearchProvider(this IServiceCollection services, string site) { services .AddOptions<StackExchangeOptions>(site) .BindConfiguration("SearchProviders:StackExchange") .Configure(o => o.Site = site); services.TryAddTransient<IValidateOptions<StackExchangeOptions>, StackExchangeOptionsValidator>(); services.AddHttpClient<ISearchProvider, StackExchangeSearchProvider>( nameof(StackExchangeSearchProvider), (httpClient, sp) => { httpClient.BaseAddress = new("https://api.stackexchange.com/2.3/"); return new StackExchangeSearchProvider( Options.Create(sp.GetRequiredService<IOptionsMonitor<StackExchangeOptions>>().Get(site)), httpClient ); } ); return services; } }