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() {