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