From b80f4d31f3d07d4d6c84bbc0e491b59d2cf4710a Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 20 Jul 2016 16:52:22 -0700 Subject: [PATCH] Add a test to verify our service lifetimes This test uses the new features in the service provider to verify that we don't reference any scoped services from singletons. Note that this can't really cover the cases where we have optional services or where we replace default services (like DI for controllers). You'll just have to be careful. --- .../MvcCoreServiceCollectionExtensionsTest.cs | 2 -- .../MvcServiceCollectionExtensionsTest.cs | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreServiceCollectionExtensionsTest.cs index f32d3606f7..0aa7b31b7a 100644 --- a/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Core.Test/DependencyInjection/MvcCoreServiceCollectionExtensionsTest.cs @@ -7,8 +7,6 @@ using System.Linq; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ActionConstraints; using Microsoft.AspNetCore.Mvc.ApplicationModels; -using Microsoft.AspNetCore.Mvc.Controllers; -using Microsoft.AspNetCore.Mvc.Core; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Routing; diff --git a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs index bf8cf2e720..4ad71818eb 100644 --- a/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Test/MvcServiceCollectionExtensionsTest.cs @@ -33,6 +33,8 @@ using Moq; using Newtonsoft.Json.Serialization; using Newtonsoft.Json; using Xunit; +using Microsoft.Extensions.ObjectPool; +using System.Diagnostics; namespace Microsoft.AspNetCore.Mvc { @@ -245,6 +247,34 @@ namespace Microsoft.AspNetCore.Mvc Assert.Single(services, d => d.ServiceType == typeof(IConfigureOptions)); } + [Fact] + public void AddMvc_NoScopedServiceIsReferredToByASingleton() + { + // Arrange + var services = new ServiceCollection(); + + services.AddSingleton(GetHostingEnvironment()); + services.AddSingleton(); + services.AddSingleton(new DiagnosticListener("Microsoft.AspNet")); + services.AddLogging(); + services.AddOptions(); + services.AddMvc(); + + var root = services.BuildServiceProvider(validateScopes: true); + + var scopeFactory = root.GetRequiredService(); + + // Act & Assert + using (var scope = scopeFactory.CreateScope()) + { + foreach (var serviceType in services.Select(d => d.ServiceType).Where(t => !t.GetTypeInfo().IsGenericTypeDefinition).Distinct()) + { + // This will throw if something is invalid. + scope.ServiceProvider.GetService(typeof(IEnumerable<>).MakeGenericType(serviceType)); + } + } + } + private IEnumerable SingleRegistrationServiceTypes { get