From b1054a28c762da1bce345ce0e043da0092362452 Mon Sep 17 00:00:00 2001 From: jacalvar Date: Tue, 17 May 2016 17:31:29 -0700 Subject: [PATCH] [Fixes #4353] Cleanup application part discovery --- .../MvcCoreServiceCollectionExtensions.cs | 2 +- .../MvcRazorMvcCoreBuilderExtensionsTest.cs | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs index d897d2953e..162b986b59 100644 --- a/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNetCore.Mvc.Core/DependencyInjection/MvcCoreServiceCollectionExtensions.cs @@ -69,7 +69,7 @@ namespace Microsoft.Extensions.DependencyInjection manager = new ApplicationPartManager(); var environment = GetServiceFromCollection(services); - if (environment == null) + if (string.IsNullOrEmpty(environment?.ApplicationName)) { return manager; } diff --git a/test/Microsoft.AspNetCore.Mvc.Razor.Test/DependencyInjection/MvcRazorMvcCoreBuilderExtensionsTest.cs b/test/Microsoft.AspNetCore.Mvc.Razor.Test/DependencyInjection/MvcRazorMvcCoreBuilderExtensionsTest.cs index fc134364ed..e57c5828d5 100644 --- a/test/Microsoft.AspNetCore.Mvc.Razor.Test/DependencyInjection/MvcRazorMvcCoreBuilderExtensionsTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.Razor.Test/DependencyInjection/MvcRazorMvcCoreBuilderExtensionsTest.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; +using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Internal; using Microsoft.AspNetCore.Mvc.Razor.Internal; @@ -10,12 +11,50 @@ using Microsoft.AspNetCore.Mvc.Razor.TagHelpers; using Microsoft.AspNetCore.Razor.Runtime.TagHelpers; using Microsoft.AspNetCore.Razor.TagHelpers; using Microsoft.Extensions.DependencyInjection; +using Moq; using Xunit; namespace Microsoft.AspNetCore.Mvc.Razor.Test.DependencyInjection { public class MvcRazorMvcCoreBuilderExtensionsTest { + [Fact] + public void AddMvcCore_OnServiceCollectionWithoutIHostingEnvironmentInstance_DoesNotDiscoverApplicationParts() + { + // Arrange + var services = new ServiceCollection(); + + // Act + var builder = services + .AddMvcCore(); + + // Assert + Assert.Empty(builder.PartManager.ApplicationParts); + } + + [Theory] + [InlineData("")] + [InlineData(null)] + public void AddMvcCore_OnServiceCollectionWithIHostingEnvironmentInstanceWithInvalidApplicationName_DoesNotDiscoverApplicationParts(string applicationName) + { + // Arrange + var services = new ServiceCollection(); + + var hostingEnvironment = new Mock(); + hostingEnvironment + .Setup(h => h.ApplicationName) + .Returns(applicationName); + + services.AddSingleton(hostingEnvironment.Object); + + // Act + var builder = services + .AddMvcCore(); + + // Assert + Assert.Empty(builder.PartManager.ApplicationParts); + } + [Fact] public void AddTagHelpersAsServices_ReplacesTagHelperActivatorAndTagHelperTypeResolver() {