-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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 all 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.StartupObject"; | ||
private readonly object _startupKey = new object(); | ||
|
||
private AggregateException? _hostingStartupErrors; | ||
|
@@ -170,13 +170,15 @@ 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)) | ||
{ | ||
_builder.Properties.Remove(_startupConfigName); | ||
UseStartup(startupType, context, services); | ||
} | ||
}); | ||
|
@@ -193,16 +195,18 @@ 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)) | ||
{ | ||
_builder.Properties.Remove(_startupConfigName); | ||
var webHostBuilderContext = GetWebHostBuilderContext(context); | ||
var instance = startupFactory(webHostBuilderContext) ?? throw new InvalidOperationException("The specified factory returned null startup instance."); | ||
UseStartup(instance.GetType(), context, services, instance); | ||
|
@@ -316,12 +320,14 @@ 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)) | ||
{ | ||
_builder.Properties.Remove(_startupConfigName); | ||
services.Configure<GenericWebHostServiceOptions>(options => | ||
{ | ||
options.ConfigureApplication = configure; | ||
|
@@ -339,12 +345,14 @@ 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)) | ||
{ | ||
_builder.Properties.Remove(_startupConfigName); | ||
services.Configure<GenericWebHostServiceOptions>(options => | ||
{ | ||
var webhostBuilderContext = GetWebHostBuilderContext(context); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,7 @@ internal virtual WebApplicationFactory<TEntryPoint> WithWebHostBuilderCore(Actio | |
var factory = new DelegatedWebApplicationFactory( | ||
ClientOptions, | ||
CreateServer, | ||
CreateServer, | ||
CreateHost, | ||
CreateWebHostBuilder, | ||
CreateHostBuilder, | ||
|
@@ -337,7 +338,10 @@ private void ConfigureHostBuilder(IHostBuilder hostBuilder) | |
} | ||
else | ||
{ | ||
webHostBuilder.UseTestServer(); | ||
webHostBuilder.ConfigureServices(services => | ||
{ | ||
services.AddSingleton<IServer>(CreateServer); | ||
}); | ||
} | ||
}); | ||
_host = CreateHost(hostBuilder); | ||
|
@@ -565,10 +569,19 @@ private static void EnsureDepsFile() | |
/// <returns>The <see cref="TestServer"/> with the bootstrapped application.</returns> | ||
protected virtual TestServer CreateServer(IWebHostBuilder builder) => new(builder); | ||
|
||
/// <summary> | ||
/// Creates the <see cref="TestServer"/> with the <see cref="IServiceProvider"/> from the bootstrapped application. | ||
/// This is only called for applications using <see cref="IHostBuilder"/>. Applications based on | ||
/// <see cref="IWebHostBuilder"/> will use <see cref="CreateHost"/> instead. | ||
/// </summary> | ||
/// <param name="serviceProvider">The <see cref="IServiceProvider"/> from the bootstrapped application.</param> | ||
/// <returns></returns> | ||
protected virtual TestServer CreateServer(IServiceProvider serviceProvider) => new(serviceProvider); | ||
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. We can remove this and consider it later if we want, as it introduces a new API. 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. I'm inclined to keep it even though introducing a new API in an obsoletion pass feels funky since we don't expose any other nice ways for users to customize TestServer with a finalized DI container and we've gotten feedback in the past about how hard it is to plugin WAF APIs into different points in the minimal host's lifecycle (mostly before and after IHostBuilder.Build, but still). 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. Opened issue #62782 for this, but I'll go ahead and merge for now so we can proceed with obsoleting |
||
|
||
/// <summary> | ||
/// Creates the <see cref="IHost"/> with the bootstrapped application in <paramref name="builder"/>. | ||
/// This is only called for applications using <see cref="IHostBuilder"/>. Applications based on | ||
/// <see cref="IWebHostBuilder"/> will use <see cref="CreateServer"/> instead. | ||
/// <see cref="IWebHostBuilder"/> will use <see cref="CreateServer(IWebHostBuilder)"/> instead. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IHostBuilder"/> used to create the host.</param> | ||
/// <returns>The <see cref="IHost"/> with the bootstrapped application.</returns> | ||
|
@@ -801,6 +814,7 @@ public virtual async ValueTask DisposeAsync() | |
private sealed class DelegatedWebApplicationFactory : WebApplicationFactory<TEntryPoint> | ||
{ | ||
private readonly Func<IWebHostBuilder, TestServer> _createServer; | ||
private readonly Func<IServiceProvider, TestServer> _createServerFromServiceProvider; | ||
private readonly Func<IHostBuilder, IHost> _createHost; | ||
private readonly Func<IWebHostBuilder?> _createWebHostBuilder; | ||
private readonly Func<IHostBuilder?> _createHostBuilder; | ||
|
@@ -810,6 +824,7 @@ private sealed class DelegatedWebApplicationFactory : WebApplicationFactory<TEnt | |
public DelegatedWebApplicationFactory( | ||
WebApplicationFactoryClientOptions options, | ||
Func<IWebHostBuilder, TestServer> createServer, | ||
Func<IServiceProvider, TestServer> createServerFromServiceProvider, | ||
Func<IHostBuilder, IHost> createHost, | ||
Func<IWebHostBuilder?> createWebHostBuilder, | ||
Func<IHostBuilder?> createHostBuilder, | ||
|
@@ -819,6 +834,7 @@ public DelegatedWebApplicationFactory( | |
{ | ||
ClientOptions = new WebApplicationFactoryClientOptions(options); | ||
_createServer = createServer; | ||
_createServerFromServiceProvider = createServerFromServiceProvider; | ||
_createHost = createHost; | ||
_createWebHostBuilder = createWebHostBuilder; | ||
_createHostBuilder = createHostBuilder; | ||
|
@@ -829,6 +845,8 @@ public DelegatedWebApplicationFactory( | |
|
||
protected override TestServer CreateServer(IWebHostBuilder builder) => _createServer(builder); | ||
|
||
protected override TestServer CreateServer(IServiceProvider serviceProvider) => _createServerFromServiceProvider(serviceProvider); | ||
|
||
protected override IHost CreateHost(IHostBuilder builder) => _createHost(builder); | ||
|
||
protected override IWebHostBuilder? CreateWebHostBuilder() => _createWebHostBuilder(); | ||
|
@@ -846,6 +864,7 @@ internal override WebApplicationFactory<TEntryPoint> WithWebHostBuilderCore(Acti | |
return new DelegatedWebApplicationFactory( | ||
ClientOptions, | ||
_createServer, | ||
_createServerFromServiceProvider, | ||
_createHost, | ||
_createWebHostBuilder, | ||
_createHostBuilder, | ||
|
Uh oh!
There was an error while loading. Please reload this page.