Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 13 additions & 9 deletions src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Hosting;

internal sealed class GenericWebHostBuilder : WebHostBuilderBase, ISupportsStartup
{
private object? _startupObject;
private const string _startupConfigName = "__UseStartup.StartupType";
private readonly object _startupKey = new object();

private AggregateException? _hostingStartupErrors;
Expand Down Expand Up @@ -170,12 +170,13 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

// UseStartup can be called multiple times. Only run the last one.
_startupObject = startupType;
_builder.Properties[_startupConfigName] = startupType;

_builder.ConfigureServices((context, services) =>
{
// Run this delegate if the startup type matches
if (object.ReferenceEquals(_startupObject, startupType))
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug introduced by #24144 that allowed multiple startups to run if they spanned different IWebHostBuilder instances. See test MultipleConfigureWebHostCallsWithUseStartupLastWins below for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crazy

object.ReferenceEquals(startupObject, startupType))
{
UseStartup(startupType, context, services);
}
Expand All @@ -193,15 +194,16 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

// Clear the startup type
_startupObject = startupFactory;
_builder.Properties[_startupConfigName] = startupFactory;

_builder.ConfigureServices(ConfigureStartup);

[UnconditionalSuppressMessage("Trimmer", "IL2072", Justification = "Startup type created by factory can't be determined statically.")]
void ConfigureStartup(HostBuilderContext context, IServiceCollection services)
{
// UseStartup can be called multiple times. Only run the last one.
if (object.ReferenceEquals(_startupObject, startupFactory))
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
object.ReferenceEquals(startupObject, startupFactory))
{
var webHostBuilderContext = GetWebHostBuilderContext(context);
var instance = startupFactory(webHostBuilderContext) ?? throw new InvalidOperationException("The specified factory returned null startup instance.");
Expand Down Expand Up @@ -316,11 +318,12 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configure)
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

// Clear the startup type
_startupObject = configure;
_builder.Properties[_startupConfigName] = configure;

_builder.ConfigureServices((context, services) =>
{
if (object.ReferenceEquals(_startupObject, configure))
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
object.ReferenceEquals(startupObject, configure))
{
services.Configure<GenericWebHostServiceOptions>(options =>
{
Expand All @@ -339,11 +342,12 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

// Clear the startup type
_startupObject = configure;
_builder.Properties[_startupConfigName] = configure;

_builder.ConfigureServices((context, services) =>
{
if (object.ReferenceEquals(_startupObject, configure))
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
object.ReferenceEquals(startupObject, configure))
{
services.Configure<GenericWebHostServiceOptions>(options =>
{
Expand Down
21 changes: 13 additions & 8 deletions src/Mvc/perf/benchmarkapps/BasicApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
Expand Down Expand Up @@ -229,25 +230,29 @@ private void DropDatabaseTables(IServiceProvider services)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
}
23 changes: 14 additions & 9 deletions src/Mvc/perf/benchmarkapps/BasicViews/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
Expand Down Expand Up @@ -191,26 +192,30 @@ private void DropDatabaseTables(IServiceProvider services)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
}
21 changes: 13 additions & 8 deletions src/Mvc/perf/benchmarkapps/RazorRendering/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Html;
Expand Down Expand Up @@ -60,24 +61,28 @@ private static List<DataB> GenerateDataB()

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
28 changes: 17 additions & 11 deletions src/Mvc/samples/MvcSandbox/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;

namespace MvcSandbox;

public class Startup
Expand Down Expand Up @@ -42,22 +44,26 @@ static void ConfigureEndpoints(IEndpointRouteBuilder endpoints)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHostBuilder(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging(factory =>
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
factory
.AddConsole()
.AddDebug();
})
.UseKestrel()
.UseStartup<Startup>();
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging(factory =>
{
factory
.AddConsole()
.AddDebug();
})
.UseKestrel()
.UseStartup<Startup>();
});
}

19 changes: 12 additions & 7 deletions src/Mvc/test/WebSites/ApiExplorerWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ApiExplorerWebSite.Controllers;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Hosting;

namespace ApiExplorerWebSite;

Expand Down Expand Up @@ -45,17 +46,21 @@ public void Configure(IApplicationBuilder app)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHostBuilder(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>();
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>();
});
}

20 changes: 13 additions & 7 deletions src/Mvc/test/WebSites/ApplicationModelWebSite/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;

namespace ApplicationModelWebSite;

public class Startup
Expand Down Expand Up @@ -34,17 +36,21 @@ public void Configure(IApplicationBuilder app)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHostBuilder(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
});
}

29 changes: 19 additions & 10 deletions src/Mvc/test/WebSites/BasicWebSite/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;

namespace BasicWebSite;

public class Program
{
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
public static void Main(string[] args)
{
using var host = CreateHostBuilder(args).Build();
host.Run();
}

// Do not change. This is the pattern our test infrastructure uses to initialize a IWebHostBuilder from
// a users app.
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
// This method now returns IHostBuilder and uses the new pattern with HostBuilder and ConfigureWebHost
public static IHostBuilder CreateHostBuilder(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
});
}

public class TestService
Expand Down
Loading
Loading