From ec4d084d940161519778b9f2ba83b450e5e3606a Mon Sep 17 00:00:00 2001 From: lord-executor Date: Mon, 7 Feb 2022 17:35:33 +0100 Subject: [PATCH] Explicit test for injecting scoped IServiceProvider (#63225) (#64558) --- .../DependencyInjectionSpecificationTests.cs | 41 +++++++++++++++++++ .../tests/DI.External.Tests/LightInject.cs | 9 +++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection.Specification.Tests/src/DependencyInjectionSpecificationTests.cs b/src/libraries/Microsoft.Extensions.DependencyInjection.Specification.Tests/src/DependencyInjectionSpecificationTests.cs index 8a4d73684b418..c607b2a37ed1c 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection.Specification.Tests/src/DependencyInjectionSpecificationTests.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection.Specification.Tests/src/DependencyInjectionSpecificationTests.cs @@ -122,6 +122,47 @@ public void TransientServiceCanBeResolvedFromScope() } } + [Theory] + [InlineData(ServiceLifetime.Scoped)] + [InlineData(ServiceLifetime.Transient)] + public void NonSingletonService_WithInjectedProvider_ResolvesScopeProvider(ServiceLifetime lifetime) + { + // Arrange + var collection = new TestServiceCollection(); + collection.AddScoped(); + collection.Add(new ServiceDescriptor(typeof(ClassWithServiceProvider), typeof(ClassWithServiceProvider), lifetime)); + var provider = CreateServiceProvider(collection); + + // Act + IFakeService fakeServiceFromScope1 = null; + IFakeService otherFakeServiceFromScope1 = null; + IFakeService fakeServiceFromScope2 = null; + IFakeService otherFakeServiceFromScope2 = null; + + using (var scope1 = provider.CreateScope()) + { + var serviceWithProvider = scope1.ServiceProvider.GetRequiredService(); + fakeServiceFromScope1 = serviceWithProvider.ServiceProvider.GetRequiredService(); + + serviceWithProvider = scope1.ServiceProvider.GetRequiredService(); + otherFakeServiceFromScope1 = serviceWithProvider.ServiceProvider.GetRequiredService(); + } + + using (var scope2 = provider.CreateScope()) + { + var serviceWithProvider = scope2.ServiceProvider.GetRequiredService(); + fakeServiceFromScope2 = serviceWithProvider.ServiceProvider.GetRequiredService(); + + serviceWithProvider = scope2.ServiceProvider.GetRequiredService(); + otherFakeServiceFromScope2 = serviceWithProvider.ServiceProvider.GetRequiredService(); + } + + // Assert + Assert.Same(fakeServiceFromScope1, otherFakeServiceFromScope1); + Assert.Same(fakeServiceFromScope2, otherFakeServiceFromScope2); + Assert.NotSame(fakeServiceFromScope1, fakeServiceFromScope2); + } + [Fact] public void SingletonServiceCanBeResolvedFromScope() { diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.External.Tests/LightInject.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.External.Tests/LightInject.cs index d9c874958e63b..2c08457b6c2e2 100644 --- a/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.External.Tests/LightInject.cs +++ b/src/libraries/Microsoft.Extensions.DependencyInjection/tests/DI.External.Tests/LightInject.cs @@ -8,11 +8,16 @@ namespace Microsoft.Extensions.DependencyInjection.Specification { - public class LightInjectDependencyInjectionSpecificationTests: DependencyInjectionSpecificationTests + public class LightInjectDependencyInjectionSpecificationTests : SkippableDependencyInjectionSpecificationTests { public override bool SupportsIServiceProviderIsService => false; - protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) + public override string[] SkippedTests => new string[] + { + "NonSingletonService_WithInjectedProvider_ResolvesScopeProvider" + }; + + protected override IServiceProvider CreateServiceProviderImpl(IServiceCollection serviceCollection) { var builder = new ContainerBuilder(); builder.Populate(serviceCollection);