From eebc7141155cb0ae5ddfe81403424fd0c7337b8e Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 1 Nov 2026 17:44:39 +0100 Subject: [PATCH] Simple Injector sample: .NET 10, SimpleInjector 5.6.0, lifestyle mismatch test Retarget both projects to net10.0, update SimpleInjector to 5.6.0 and the test packages, use target-typed new and an expression-bodied property in ContainerManager, and add a test that pins the Verify() lifestyle mismatch failure. --- .../SimpleInjectorExample/ContainerManager.cs | 4 ++-- .../SimpleInjectorExample.csproj | 4 ++-- .../Tests/ContainerManagerTests.cs | 17 ++++++++++++++++- .../Tests/Tests.csproj | 10 +++++----- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/ContainerManager.cs b/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/ContainerManager.cs index fee97a39e9..ee22dce206 100644 --- a/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/ContainerManager.cs +++ b/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/ContainerManager.cs @@ -5,7 +5,7 @@ public class ContainerManager { - private static readonly Lazy _container = new Lazy(ConfigureServices); + private static readonly Lazy _container = new(ConfigureServices); private static Container ConfigureServices() { @@ -20,5 +20,5 @@ private static Container ConfigureServices() return container; } - public static Container Instance { get => _container.Value; } + public static Container Instance => _container.Value; } \ No newline at end of file diff --git a/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/SimpleInjectorExample.csproj b/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/SimpleInjectorExample.csproj index 19510f087b..01bb0d8fc4 100644 --- a/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/SimpleInjectorExample.csproj +++ b/dependency-injection-tools/SimpleInjectorImplementationConsole/SimpleInjectorExample/SimpleInjectorExample.csproj @@ -1,11 +1,11 @@ Exe - net6.0 + net10.0 enable enable - + \ No newline at end of file diff --git a/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/ContainerManagerTests.cs b/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/ContainerManagerTests.cs index ef5b82b5b8..ee8964ba80 100644 --- a/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/ContainerManagerTests.cs +++ b/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/ContainerManagerTests.cs @@ -1,6 +1,7 @@ using SimpleInjectorExample.Services; using SimpleInjectorExample.Spec; +using SimpleInjector; using SimpleInjector.Lifestyles; namespace Tests; @@ -64,4 +65,18 @@ public void GivenIUserService_WhenGetInstanceIsInvoked_ThenReturnsUserService() Assert.Equal(expectedType,service.GetType()); } -} \ No newline at end of file + [Fact] + public void GivenSingletonDependingOnScoped_WhenVerifyIsInvoked_ThenThrowsLifestyleMismatch() + { + var container = new Container(); + container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle(); + container.Register(Lifestyle.Singleton); + container.Register(Lifestyle.Scoped); + container.Register(Lifestyle.Singleton); + + var exception = Assert.Throws(() => container.Verify()); + + Assert.Contains("[Lifestyle Mismatch]", exception.Message); + } + +} diff --git a/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/Tests.csproj b/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/Tests.csproj index 26d403e987..7727c79421 100644 --- a/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/Tests.csproj +++ b/dependency-injection-tools/SimpleInjectorImplementationConsole/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net10.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all