Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed de-duping hosting startup assemblies #31440

Merged
merged 1 commit into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,23 @@ private void ExecuteHostingStartups()
}

var exceptions = new List<Exception>();
var processed = new HashSet<Assembly>();

_hostingStartupWebHostBuilder = new HostingStartupWebHostBuilder(this);

// Execute the hosting startup assemblies
foreach (var assemblyName in webHostOptions.GetFinalHostingStartupAssemblies().Distinct(StringComparer.OrdinalIgnoreCase))
foreach (var assemblyName in webHostOptions.GetFinalHostingStartupAssemblies())
{
try
{
var assembly = Assembly.Load(new AssemblyName(assemblyName));

if (!processed.Add(assembly))
{
// Already processed, skip it
continue;
}

foreach (var attribute in assembly.GetCustomAttributes<HostingStartupAttribute>())
{
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType)!;
Expand Down
9 changes: 8 additions & 1 deletion src/Hosting/Hosting/src/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,21 @@ private IServiceCollection BuildCommonServices(out AggregateException? hostingSt
if (!_options.PreventHostingStartup)
{
var exceptions = new List<Exception>();
var processed = new HashSet<Assembly>();

// Execute the hosting startup assemblies
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies().Distinct(StringComparer.OrdinalIgnoreCase))
foreach (var assemblyName in _options.GetFinalHostingStartupAssemblies())
{
try
{
var assembly = Assembly.Load(new AssemblyName(assemblyName));

if (!processed.Add(assembly))
{
// Already processed, skip it
continue;
}

foreach (var attribute in assembly.GetCustomAttributes<HostingStartupAttribute>())
{
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType)!;
Expand Down
21 changes: 21 additions & 0 deletions src/Hosting/Hosting/test/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,27 @@ public void Build_RunsHostingStartupAssembliesIfSpecified(IWebHostBuilder builde
using (var host = builder.Build())
{
Assert.Equal("1", builder.GetSetting("testhostingstartup1"));
Assert.Equal("1", builder.GetSetting("testhostingstartup1_calls"));
}
}

[Theory]
[MemberData(nameof(DefaultWebHostBuildersWithConfig))]
public void Build_RunsDeduplicatedHostingStartupAssembliesIfSpecified(IWebHostBuilder builder)
{
var fullName = typeof(TestStartupAssembly1.TestHostingStartup1).Assembly.FullName;
var name = typeof(TestStartupAssembly1.TestHostingStartup1).Assembly.GetName().Name;

builder = builder
.CaptureStartupErrors(false)
.UseSetting(WebHostDefaults.HostingStartupAssembliesKey, fullName + ";" + name)
.Configure(app => { })
.UseServer(new TestServer());

using (var host = builder.Build())
{
Assert.Equal("1", builder.GetSetting("testhostingstartup1"));
Assert.Equal("1", builder.GetSetting("testhostingstartup1_calls"));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Globalization;
using Microsoft.AspNetCore.Hosting;

[assembly: HostingStartup(typeof(TestStartupAssembly1.TestHostingStartup1))]
Expand All @@ -11,8 +12,17 @@ public class TestHostingStartup1 : IHostingStartup
{
public void Configure(IWebHostBuilder builder)
{
var calls = builder.GetSetting("testhostingstartup1_calls");
var numCalls = 1;

if (calls != null)
{
numCalls = int.Parse(calls, CultureInfo.InvariantCulture) + 1;
}

builder.UseSetting("testhostingstartup1", "1");
builder.UseSetting("testhostingstartup_chain", builder.GetSetting("testhostingstartup_chain") + "1");
builder.UseSetting("testhostingstartup1_calls", numCalls.ToString(CultureInfo.InvariantCulture));
}
}
}