From fbc20af54438fc9684184ee546e71891313a282a Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 26 Aug 2019 21:53:39 -0700 Subject: [PATCH 1/2] Added verification test for resolving singleton from scoped container. --- .../DependencyInjectionSpecificationTests.cs | 32 +++++++++++++++++++ .../src/Fakes/ClassWithServiceProvider.cs | 16 ++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/DependencyInjection/DI.Specification.Tests/src/Fakes/ClassWithServiceProvider.cs diff --git a/src/DependencyInjection/DI.Specification.Tests/src/DependencyInjectionSpecificationTests.cs b/src/DependencyInjection/DI.Specification.Tests/src/DependencyInjectionSpecificationTests.cs index 4bfa9d3fb25..164d78f2cb8 100644 --- a/src/DependencyInjection/DI.Specification.Tests/src/DependencyInjectionSpecificationTests.cs +++ b/src/DependencyInjection/DI.Specification.Tests/src/DependencyInjectionSpecificationTests.cs @@ -122,6 +122,38 @@ public void TransientServiceCanBeResolvedFromScope() } } + [Fact] + public void SingletonServiceCanBeResolvedFromScope() + { + // Arrange + var collection = new TestServiceCollection(); + collection.AddSingleton(); + var provider = CreateServiceProvider(collection); + + // Act + IServiceProvider scopedSp1 = null; + IServiceProvider scopedSp2 = null; + ClassWithServiceProvider instance1 = null; + ClassWithServiceProvider instance2 = null; + + using (var scope1 = provider.CreateScope()) + { + scopedSp1 = scope1.ServiceProvider; + instance1 = scope1.ServiceProvider.GetRequiredService(); + } + + using (var scope2 = provider.CreateScope()) + { + scopedSp2 = scope2.ServiceProvider; + instance2 = scope2.ServiceProvider.GetRequiredService(); + } + + // Assert + Assert.Same(instance1.ServiceProvider, instance2.ServiceProvider); + Assert.NotSame(instance1.ServiceProvider, scopedSp1); + Assert.NotSame(instance2.ServiceProvider, scopedSp2); + } + [Fact] public void SingleServiceCanBeIEnumerableResolved() { diff --git a/src/DependencyInjection/DI.Specification.Tests/src/Fakes/ClassWithServiceProvider.cs b/src/DependencyInjection/DI.Specification.Tests/src/Fakes/ClassWithServiceProvider.cs new file mode 100644 index 00000000000..f58d02273ea --- /dev/null +++ b/src/DependencyInjection/DI.Specification.Tests/src/Fakes/ClassWithServiceProvider.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.DependencyInjection.Specification.Fakes +{ + public class ClassWithServiceProvider + { + public ClassWithServiceProvider(IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + } + + public IServiceProvider ServiceProvider { get; } + } +} From 96bb359ab3437f72c2c78fb737281a5b6d3980cc Mon Sep 17 00:00:00 2001 From: David Fowler Date: Mon, 26 Aug 2019 22:39:44 -0700 Subject: [PATCH 2/2] Skip failing test with the Unity container --- src/DependencyInjection/DI.External.Tests/test/Unity.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/DI.External.Tests/test/Unity.cs b/src/DependencyInjection/DI.External.Tests/test/Unity.cs index 56689291704..233bc5acaf8 100644 --- a/src/DependencyInjection/DI.External.Tests/test/Unity.cs +++ b/src/DependencyInjection/DI.External.Tests/test/Unity.cs @@ -7,8 +7,9 @@ namespace Microsoft.Extensions.DependencyInjection.Specification { public class UnityDependencyInjectionSpecificationTests: SkippableDependencyInjectionSpecificationTests { - public override string[] SkippedTests => new String[0] + public override string[] SkippedTests => new[] { + "SingletonServiceCanBeResolvedFromScope" }; protected override IServiceProvider CreateServiceProviderImpl(IServiceCollection serviceCollection)