Add a way to prevent specific hosting startup from runnning (#1243)
This commit is contained in:
parent
1e360cbb36
commit
e892ed8bbd
|
|
@ -8,6 +8,7 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
public static readonly string ApplicationKey = "applicationName";
|
||||
public static readonly string StartupAssemblyKey = "startupAssembly";
|
||||
public static readonly string HostingStartupAssembliesKey = "hostingStartupAssemblies";
|
||||
public static readonly string HostingStartupExcludeAssembliesKey = "hostingStartupExcludeAssemblies";
|
||||
|
||||
public static readonly string DetailedErrorsKey = "detailedErrors";
|
||||
public static readonly string EnvironmentKey = "environment";
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
|||
// Log the fact that we did load hosting startup assemblies.
|
||||
if (_logger.IsEnabled(LogLevel.Debug))
|
||||
{
|
||||
foreach (var assembly in _options.HostingStartupAssemblies)
|
||||
foreach (var assembly in _options.GetFinalHostingStartupAssemblies())
|
||||
{
|
||||
_logger.LogDebug("Loaded hosting startup assembly {assemblyName}", assembly);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Microsoft.AspNetCore.Hosting.Internal
|
||||
|
|
@ -33,8 +34,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
|||
SuppressStatusMessages = WebHostUtilities.ParseBool(configuration, WebHostDefaults.SuppressStatusMessagesKey);
|
||||
|
||||
// Search the primary assembly and configured assemblies.
|
||||
HostingStartupAssemblies = $"{ApplicationName};{configuration[WebHostDefaults.HostingStartupAssembliesKey]}"
|
||||
.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
|
||||
HostingStartupAssemblies = Split($"{ApplicationName};{configuration[WebHostDefaults.HostingStartupAssembliesKey]}");
|
||||
HostingStartupExcludeAssemblies = Split(configuration[WebHostDefaults.HostingStartupExcludeAssembliesKey]);
|
||||
|
||||
var timeout = configuration[WebHostDefaults.ShutdownTimeoutKey];
|
||||
if (!string.IsNullOrEmpty(timeout)
|
||||
|
|
@ -52,6 +53,8 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
|||
|
||||
public IReadOnlyList<string> HostingStartupAssemblies { get; set; }
|
||||
|
||||
public IReadOnlyList<string> HostingStartupExcludeAssemblies { get; set; }
|
||||
|
||||
public bool DetailedErrors { get; set; }
|
||||
|
||||
public bool CaptureStartupErrors { get; set; }
|
||||
|
|
@ -66,5 +69,28 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
|||
|
||||
public TimeSpan ShutdownTimeout { get; set; } = TimeSpan.FromSeconds(5);
|
||||
|
||||
public IEnumerable<string> GetFinalHostingStartupAssemblies()
|
||||
{
|
||||
return HostingStartupAssemblies.Except(HostingStartupExcludeAssemblies, StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private IReadOnlyList<string> Split(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
|
||||
var list = new List<string>();
|
||||
foreach (var part in value.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var trimmedPart = part;
|
||||
if (!string.IsNullOrEmpty(trimmedPart))
|
||||
{
|
||||
list.Add(trimmedPart);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -176,7 +176,7 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
|
||||
var logger = hostingServiceProvider.GetRequiredService<ILogger<WebHost>>();
|
||||
// Warn about duplicate HostingStartupAssemblies
|
||||
foreach (var assemblyName in _options.HostingStartupAssemblies.GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
|
||||
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().GroupBy(a => a, StringComparer.OrdinalIgnoreCase).Where(g => g.Count() > 1))
|
||||
{
|
||||
logger.LogWarning($"The assembly {assemblyName} was specified multiple times. Hosting startup assemblies should only be specified once.");
|
||||
}
|
||||
|
|
@ -206,7 +206,7 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
var exceptions = new List<Exception>();
|
||||
|
||||
// Execute the hosting startup assemblies
|
||||
foreach (var assemblyName in _options.HostingStartupAssemblies.Distinct(StringComparer.OrdinalIgnoreCase))
|
||||
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().Distinct(StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
try
|
||||
{
|
||||
|
|
|
|||
|
|
@ -779,6 +779,23 @@ namespace Microsoft.AspNetCore.Hosting
|
|||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_HostingStartupAssemblyCanBeExcluded()
|
||||
{
|
||||
var builder = CreateWebHostBuilder()
|
||||
.CaptureStartupErrors(false)
|
||||
.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, typeof(TestStartupAssembly1.TestHostingStartup1).GetTypeInfo().Assembly.FullName)
|
||||
.UseSetting(WebHostDefaults.HostingStartupExcludeAssembliesKey, typeof(TestStartupAssembly1.TestHostingStartup1).GetTypeInfo().Assembly.FullName)
|
||||
.Configure(app => { })
|
||||
.UseServer(new TestServer());
|
||||
|
||||
using (var host = builder.Build())
|
||||
{
|
||||
Assert.Null(builder.GetSetting("testhostingstartup1"));
|
||||
Assert.Equal("0", builder.GetSetting("testhostingstartup_chain"));
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Build_ConfigureLoggingInHostingStartupWorks()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue