public void GettingSectionsAndKeysWroks() { var keyedServiceProvider = new KeyedServiceProvider(new ServiceCollection().BuildServiceProvider()) .Add(configuration.GetSection("ShopCalculationSection"), (services, configSection, rootSp) => { services.AddSingleton <IInvoicePriceCalculator, InvoicePriceCalculator>(); services.AddConfig <InvoicePriceCalculatorOptions>(configSection); }, "Section1") .Add(configuration.GetSection("ShopCalculationSection"), (services, configSection, rootSp) => { services.AddSingleton <IInvoicePriceCalculator, InvoicePriceCalculator>(); services.AddConfig <InvoicePriceCalculatorOptions>(configSection); }, "Section2"); Assert.AreEqual(new[] { "Section1", "Section2" }, keyedServiceProvider.Sections().OrderBy(s => s).ToArray()); Assert.AreEqual(new[] { "BottleShop", "FlowerShop" }, keyedServiceProvider.Keys("Section1").OrderBy(k => k).ToList()); Assert.AreEqual(new[] { "BottleShop", "FlowerShop" }, keyedServiceProvider.Keys("Section2").OrderBy(k => k).ToList()); var keysBySection = keyedServiceProvider.Keys(); Assert.AreEqual(new[] { "Section1", "Section2" }, keysBySection.Keys.OrderBy(s => s).ToArray()); Assert.AreEqual(new[] { "BottleShop", "FlowerShop" }, keysBySection["Section1"].OrderBy(k => k).ToList()); Assert.AreEqual(new[] { "BottleShop", "FlowerShop" }, keysBySection["Section2"].OrderBy(k => k).ToList()); }
public void SectionRegistrationWorks() { var keyedServiceProvider = new KeyedServiceProvider(new ServiceCollection().BuildServiceProvider()) .Add(configuration.GetSection("ShopCalculationSection"), (services, configSection, rootSp) => { services.AddSingleton(PriceCalculatorFactory.Create); services.AddSingleton <IInvoicePriceCalculator, InvoicePriceCalculator>(); services.AddSingleton <IPostPriceCalculator, PostPriceCalculator>(); services.AddConfig <InvoicePriceCalculatorOptions>(configSection); services.AddConfig <PostPriceCalculatorOptions>(configSection); services.AddConfig <PriceCalculatorFactoryOptions>(configSection); }); var flowerShopServiceProvider = keyedServiceProvider.GetRequiredService <IServiceProvider>("ShopCalculationSection", "FlowerShop"); Assert.AreEqual(110, flowerShopServiceProvider.GetRequiredService <IPriceCalculator>().Calculate()); Assert.AreEqual(110, keyedServiceProvider.GetRequiredService <IPriceCalculator>("ShopCalculationSection", "FlowerShop").Calculate()); var dildoShopServiceProvider = keyedServiceProvider.GetRequiredService <IServiceProvider>("ShopCalculationSection", "BottleShop"); Assert.AreEqual(190, dildoShopServiceProvider.GetRequiredService <IPriceCalculator>().Calculate()); Assert.AreEqual(190, keyedServiceProvider.GetRequiredService <IPriceCalculator>("ShopCalculationSection", "BottleShop").Calculate()); }