From 748043b03fc1323b3634a4d401fe4bbb16640c46 Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Sun, 6 Apr 2025 15:13:25 -0500 Subject: [PATCH] Ensure container has IServiceProvider/ScopeFactory Testing a Registry that uses Configure()/Register() should not depend on calling Populate() for factory or scoped service descriptors to work. > StructureMap.StructureMapConfigurationException: > No default Instance is registered and cannot be automatically determined > for type 'System.IServiceProvider' Workaround: ``` var container = new Container(); container.Configure(c => { c.IncludeRegistry(); c.Populate([]); }); container.AssertConfigurationIsValid(); ``` --- .../ContainerExtensions.cs | 18 +++++----- .../StructureMapContainerTests.cs | 35 +++++++++++++++++++ 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/StructureMap.Microsoft.DependencyInjection/ContainerExtensions.cs b/src/StructureMap.Microsoft.DependencyInjection/ContainerExtensions.cs index dcad296..f917a8e 100644 --- a/src/StructureMap.Microsoft.DependencyInjection/ContainerExtensions.cs +++ b/src/StructureMap.Microsoft.DependencyInjection/ContainerExtensions.cs @@ -95,14 +95,6 @@ public static void Populate(this Registry registry, IEnumerable(); - registry.For() - .LifecycleIs(Lifecycles.Container) - .Use(); - - registry.For() - .LifecycleIs(Lifecycles.Container) - .Use(); - registry.Register(descriptors); } @@ -135,6 +127,16 @@ public static void Configure(this IProfileRegistry registry, FuncThe service descriptors. public static void Register(this IProfileRegistry registry, IEnumerable descriptors) { + // Required for factory service descriptors + registry.For() + .LifecycleIs(Lifecycles.Container) + .UseIfNone(); + + // Required for scoped service descriptors + registry.For() + .LifecycleIs(Lifecycles.Container) + .UseIfNone(); + foreach (var descriptor in descriptors) { registry.Register(descriptor); diff --git a/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapContainerTests.cs b/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapContainerTests.cs index e54464e..7793b2b 100644 --- a/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapContainerTests.cs +++ b/test/StructureMap.Microsoft.DependencyInjection.Tests/StructureMapContainerTests.cs @@ -61,6 +61,41 @@ public void ConfigureAndRegisterDoNotPreventPopulate() Assert.NotNull(container.GetInstance()); } + [Fact] + public void ConfigureDoesNotRequirePopulate() + { + var container = new Container(); + container.Configure(config => + { + config.Configure(services => services + .AddScoped(_ => new FakeService()) + ); + }); + + Assert.NotNull(container.GetInstance()); + + Assert.NotNull(container.GetInstance()); + Assert.NotNull(container.GetInstance()); + } + + [Fact] + public void RegisterDoesNotRequirePopulate() + { + var container = new Container(); + container.Configure(config => + { + var services = new ServiceCollection() + .AddScoped(_ => new FakeService()); + + config.Register(services); + }); + + Assert.NotNull(container.GetInstance()); + + Assert.NotNull(container.GetInstance()); + Assert.NotNull(container.GetInstance()); + } + [Theory] [InlineData(true)] [InlineData(false)]