public void Our_ServiceRegister_maps_all_to_the_same_instance(bool scoped) { IServiceCollection services = new ServiceCollection(); var reg = new StObjContextRoot.ServiceRegister(TestHelper.Monitor, services); reg.Register(typeof(IA), typeof(A), scoped, allowMultipleRegistration: false); reg.Register(typeof(IB), typeof(A), scoped, allowMultipleRegistration: false); var sp = services.BuildServiceProvider(); var a = sp.GetRequiredService <IA>(); var b = sp.GetRequiredService <IB>(); var c = sp.GetRequiredService <A>(); a.Should().BeSameAs(b).And.BeSameAs(c); using (var scope = sp.CreateScope()) { var aS = scope.ServiceProvider.GetRequiredService <IA>(); var bS = scope.ServiceProvider.GetRequiredService <IB>(); var cS = scope.ServiceProvider.GetRequiredService <A>(); aS.Should().BeSameAs(bS).And.BeSameAs(cS); if (scoped) { a.Should().NotBeSameAs(aS); } else { a.Should().BeSameAs(aS); } } }
void ConfigureServices(StObjContextRoot.ServiceRegister register, TotallyExternalStartupServiceThatActAsAConfiguratorOfTheWholeSystem conf) { var impl = conf.AlwaysUseAlice ? typeof(PrivateAlwaysAliceProvider) : typeof(PrivateAliceOrBobProvider); register.Register(typeof(IAliceOrBobProvider), impl, isScoped: true, allowMultipleRegistration: false); }