Detect duplicate startups in HostingStartupAssemblies (#1183)

This commit is contained in:
Justin Kotalik 2017-09-18 11:47:34 -07:00 committed by GitHub
parent c24c717eee
commit 500668619f
3 changed files with 33 additions and 2 deletions

View File

@ -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<Exception>();
// Execute the hosting startup assemblies
foreach (var assemblyName in _options.HostingStartupAssemblies)
foreach (var assemblyName in _options.HostingStartupAssemblies.Distinct(StringComparer.OrdinalIgnoreCase))
{
try
{

View File

@ -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
{

View File

@ -234,6 +234,36 @@ namespace Microsoft.AspNetCore.Hosting
}
}
[Fact]
public async Task MultipleStartupAssembliesSpecifiedOnlyAddAssemblyOnce()
{
var provider = new TestLoggerProvider();
var assemblyName = "RandomName";
var data = new Dictionary<string, string>
{
{ 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()
{