Skip to content

Commit 71f4919

Browse files
committed
fixup
1 parent 749289e commit 71f4919

File tree

22 files changed

+60
-56
lines changed

22 files changed

+60
-56
lines changed

src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Hosting;
2020

2121
internal sealed class GenericWebHostBuilder : WebHostBuilderBase, ISupportsStartup
2222
{
23-
private object? _startupObject;
23+
private const string _startupConfigName = "__UseStartup.StartupType";
2424
private readonly object _startupKey = new object();
2525

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

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

175175
_builder.ConfigureServices((context, services) =>
176176
{
177177
// Run this delegate if the startup type matches
178-
if (object.ReferenceEquals(_startupObject, startupType))
178+
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
179+
object.ReferenceEquals(startupObject, startupType))
179180
{
180181
UseStartup(startupType, context, services);
181182
}
@@ -193,15 +194,16 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
193194
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);
194195

195196
// Clear the startup type
196-
_startupObject = startupFactory;
197+
_builder.Properties[_startupConfigName] = startupFactory;
197198

198199
_builder.ConfigureServices(ConfigureStartup);
199200

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

318320
// Clear the startup type
319-
_startupObject = configure;
321+
_builder.Properties[_startupConfigName] = configure;
320322

321323
_builder.ConfigureServices((context, services) =>
322324
{
323-
if (object.ReferenceEquals(_startupObject, configure))
325+
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
326+
object.ReferenceEquals(startupObject, configure))
324327
{
325328
services.Configure<GenericWebHostServiceOptions>(options =>
326329
{
@@ -339,11 +342,12 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
339342
UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);
340343

341344
// Clear the startup type
342-
_startupObject = configure;
345+
_builder.Properties[_startupConfigName] = configure;
343346

344347
_builder.ConfigureServices((context, services) =>
345348
{
346-
if (object.ReferenceEquals(_startupObject, configure))
349+
if (_builder.Properties.TryGetValue(_startupConfigName, out var startupObject) &&
350+
object.ReferenceEquals(startupObject, configure))
347351
{
348352
services.Configure<GenericWebHostServiceOptions>(options =>
349353
{

src/Mvc/samples/MvcSandbox/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ static void ConfigureEndpoints(IEndpointRouteBuilder endpoints)
4444

4545
public static void Main(string[] args)
4646
{
47-
using var host = CreateHost(args)
47+
using var host = CreateHostBuilder(args)
4848
.Build();
4949

5050
host.Run();
5151
}
5252

53-
public static IHostBuilder CreateHost(string[] args) =>
53+
public static IHostBuilder CreateHostBuilder(string[] args) =>
5454
new HostBuilder()
5555
.ConfigureWebHost(webHostBuilder =>
5656
{

src/Mvc/test/WebSites/ApiExplorerWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public void Configure(IApplicationBuilder app)
4646

4747
public static void Main(string[] args)
4848
{
49-
using var host = CreateHost(args)
49+
using var host = CreateHostBuilder(args)
5050
.Build();
5151

5252
host.Run();
5353
}
5454

55-
public static IHostBuilder CreateHost(string[] args) =>
55+
public static IHostBuilder CreateHostBuilder(string[] args) =>
5656
new HostBuilder()
5757
.ConfigureWebHost(webHostBuilder =>
5858
{

src/Mvc/test/WebSites/ApplicationModelWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public void Configure(IApplicationBuilder app)
3636

3737
public static void Main(string[] args)
3838
{
39-
using var host = CreateHost(args)
39+
using var host = CreateHostBuilder(args)
4040
.Build();
4141

4242
host.Run();
4343
}
4444

45-
public static IHostBuilder CreateHost(string[] args) =>
45+
public static IHostBuilder CreateHostBuilder(string[] args) =>
4646
new HostBuilder()
4747
.ConfigureWebHost(webHostBuilder =>
4848
{

src/Mvc/test/WebSites/BasicWebSite/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Extensions.Hosting;
@@ -9,12 +9,12 @@ public class Program
99
{
1010
public static void Main(string[] args)
1111
{
12-
using var host = CreateHost(args).Build();
12+
using var host = CreateHostBuilder(args).Build();
1313
host.Run();
1414
}
1515

1616
// This method now returns IHostBuilder and uses the new pattern with HostBuilder and ConfigureWebHost
17-
public static IHostBuilder CreateHost(string[] args) =>
17+
public static IHostBuilder CreateHostBuilder(string[] args) =>
1818
new HostBuilder()
1919
.ConfigureWebHost(webHostBuilder =>
2020
{

src/Mvc/test/WebSites/ControllersFromServicesWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ public void Configure(IApplicationBuilder app)
6262

6363
public static void Main(string[] args)
6464
{
65-
using var host = CreateHost(args)
65+
using var host = CreateHostBuilder(args)
6666
.Build();
6767

6868
host.Run();
6969
}
7070

71-
public static IHostBuilder CreateHost(string[] args) =>
71+
public static IHostBuilder CreateHostBuilder(string[] args) =>
7272
new HostBuilder()
7373
.ConfigureWebHost(webHostBuilder =>
7474
{

src/Mvc/test/WebSites/CorsWebSite/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Extensions.Hosting;
@@ -9,13 +9,13 @@ public class Program
99
{
1010
public static void Main(string[] args)
1111
{
12-
using var host = CreateHost(args)
12+
using var host = CreateHostBuilder(args)
1313
.Build();
1414

1515
host.Run();
1616
}
1717

18-
public static IHostBuilder CreateHost(string[] args) =>
18+
public static IHostBuilder CreateHostBuilder(string[] args) =>
1919
new HostBuilder()
2020
.ConfigureWebHost(webHostBuilder =>
2121
{

src/Mvc/test/WebSites/ErrorPageMiddlewareWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public void Configure(IApplicationBuilder app)
2626

2727
public static void Main(string[] args)
2828
{
29-
using var host = CreateHost(args)
29+
using var host = CreateHostBuilder(args)
3030
.Build();
3131

3232
host.Run();
3333
}
3434

35-
public static IHostBuilder CreateHost(string[] args) =>
35+
public static IHostBuilder CreateHostBuilder(string[] args) =>
3636
new HostBuilder()
3737
.ConfigureWebHost(webHostBuilder =>
3838
{

src/Mvc/test/WebSites/FilesWebSite/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ public void Configure(IApplicationBuilder app)
2525

2626
public static void Main(string[] args)
2727
{
28-
using var host = CreateHost(args)
28+
using var host = CreateHostBuilder(args)
2929
.Build();
3030

3131
host.Run();
3232
}
3333

34-
public static IHostBuilder CreateHost(string[] args) =>
34+
public static IHostBuilder CreateHostBuilder(string[] args) =>
3535
new HostBuilder()
3636
.ConfigureWebHost(webHostBuilder =>
3737
{

src/Mvc/test/WebSites/FormatterWebSite/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.Extensions.Hosting;
@@ -9,13 +9,13 @@ public class Program
99
{
1010
public static void Main(string[] args)
1111
{
12-
using var host = CreateHost(args)
12+
using var host = CreateHostBuilder(args)
1313
.Build();
1414

1515
host.Run();
1616
}
1717

18-
public static IHostBuilder CreateHost(string[] args) =>
18+
public static IHostBuilder CreateHostBuilder(string[] args) =>
1919
new HostBuilder()
2020
.ConfigureWebHost(webHostBuilder =>
2121
{

0 commit comments

Comments
 (0)