-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Replace WebHostBuilder with HostBuilder pattern in MVC folder #62703
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
Changes from 5 commits
02299e4
23e48c6
531bb5f
749289e
71f4919
c5f4ac0
802ef7e
e2a1342
d15eabd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Crazy |
||
object.ReferenceEquals(startupObject, startupType)) | ||
{ | ||
UseStartup(startupType, context, services); | ||
} | ||
|
@@ -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."); | ||
|
@@ -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 => | ||
{ | ||
|
@@ -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 => | ||
{ | ||
|
Uh oh!
There was an error while loading. Please reload this page.