From 500668619fc790c43ccbb5ab399bdbbcf3955414 Mon Sep 17 00:00:00 2001 From: Justin Kotalik Date: Mon, 18 Sep 2017 11:47:34 -0700 Subject: [PATCH] Detect duplicate startups in HostingStartupAssemblies (#1183) --- .../WebHostBuilder.cs | 3 +- .../ConfigureBuilderTests.cs | 2 +- .../WebHostBuilderTests.cs | 30 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs index e5bb97ebb7..32a0093fcc 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using System.Reflection; using System.Runtime.ExceptionServices; using Microsoft.AspNetCore.Hosting.Builder; @@ -195,7 +196,7 @@ namespace Microsoft.AspNetCore.Hosting var exceptions = new List(); // Execute the hosting startup assemblies - foreach (var assemblyName in _options.HostingStartupAssemblies) + foreach (var assemblyName in _options.HostingStartupAssemblies.Distinct(StringComparer.OrdinalIgnoreCase)) { try { diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/ConfigureBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/ConfigureBuilderTests.cs index 0282627e20..cea4235c8c 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/ConfigureBuilderTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/ConfigureBuilderTests.cs @@ -9,7 +9,7 @@ using Microsoft.AspNetCore.Hosting.Internal; using Microsoft.Extensions.DependencyInjection; using Xunit; -namespace Microsoft.AspNetCore.Hosting.Tests +namespace Microsoft.AspNetCore.Hosting.Tests { public class ConfigureBuilderTests { diff --git a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs index e90f3a30d8..7007b24236 100644 --- a/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs +++ b/test/Microsoft.AspNetCore.Hosting.Tests/WebHostBuilderTests.cs @@ -234,6 +234,36 @@ namespace Microsoft.AspNetCore.Hosting } } + [Fact] + public async Task MultipleStartupAssembliesSpecifiedOnlyAddAssemblyOnce() + { + var provider = new TestLoggerProvider(); + var assemblyName = "RandomName"; + var data = new Dictionary + { + { WebHostDefaults.ApplicationKey, assemblyName }, + { WebHostDefaults.HostingStartupAssembliesKey, assemblyName } + }; + var config = new ConfigurationBuilder().AddInMemoryCollection(data).Build(); + + var builder = CreateWebHostBuilder() + .UseConfiguration(config) + .ConfigureLogging((_, factory) => + { + factory.AddProvider(provider); + }) + .UseServer(new TestServer()); + + // Verify that there was only one exception throw rather than two. + using (var host = (WebHost)builder.Build()) + { + await host.StartAsync(); + var context = provider.Sink.Writes.Where(s => s.EventId.Id == LoggerEventIds.HostingStartupAssemblyException); + Assert.NotNull(context); + Assert.Single(context); + } + } + [Fact] public void HostingContextContainsAppConfigurationDuringConfigureLogging() {