@@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Hosting;
20
20
21
21
internal sealed class GenericWebHostBuilder : WebHostBuilderBase , ISupportsStartup
22
22
{
23
- private object ? _startupObject ;
23
+ private const string _startupConfigName = "__UseStartup.StartupObject" ;
24
24
private readonly object _startupKey = new object ( ) ;
25
25
26
26
private AggregateException ? _hostingStartupErrors ;
@@ -170,13 +170,15 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
170
170
UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
171
171
172
172
// UseStartup can be called multiple times. Only run the last one.
173
- _startupObject = startupType ;
173
+ _builder . Properties [ _startupConfigName ] = startupType ;
174
174
175
175
_builder . ConfigureServices ( ( context , services ) =>
176
176
{
177
177
// 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 ) )
179
180
{
181
+ _builder . Properties . Remove ( _startupConfigName ) ;
180
182
UseStartup ( startupType , context , services ) ;
181
183
}
182
184
} ) ;
@@ -193,16 +195,18 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
193
195
UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
194
196
195
197
// Clear the startup type
196
- _startupObject = startupFactory ;
198
+ _builder . Properties [ _startupConfigName ] = startupFactory ;
197
199
198
200
_builder . ConfigureServices ( ConfigureStartup ) ;
199
201
200
202
[ UnconditionalSuppressMessage ( "Trimmer" , "IL2072" , Justification = "Startup type created by factory can't be determined statically." ) ]
201
203
void ConfigureStartup ( HostBuilderContext context , IServiceCollection services )
202
204
{
203
205
// UseStartup can be called multiple times. Only run the last one.
204
- if ( object . ReferenceEquals ( _startupObject , startupFactory ) )
206
+ if ( _builder . Properties . TryGetValue ( _startupConfigName , out var startupObject ) &&
207
+ object . ReferenceEquals ( startupObject , startupFactory ) )
205
208
{
209
+ _builder . Properties . Remove ( _startupConfigName ) ;
206
210
var webHostBuilderContext = GetWebHostBuilderContext ( context ) ;
207
211
var instance = startupFactory ( webHostBuilderContext ) ?? throw new InvalidOperationException ( "The specified factory returned null startup instance." ) ;
208
212
UseStartup ( instance . GetType ( ) , context , services , instance ) ;
@@ -316,12 +320,14 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configure)
316
320
UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
317
321
318
322
// Clear the startup type
319
- _startupObject = configure ;
323
+ _builder . Properties [ _startupConfigName ] = configure ;
320
324
321
325
_builder . ConfigureServices ( ( context , services ) =>
322
326
{
323
- if ( object . ReferenceEquals ( _startupObject , configure ) )
327
+ if ( _builder . Properties . TryGetValue ( _startupConfigName , out var startupObject ) &&
328
+ object . ReferenceEquals ( startupObject , configure ) )
324
329
{
330
+ _builder . Properties . Remove ( _startupConfigName ) ;
325
331
services . Configure < GenericWebHostServiceOptions > ( options =>
326
332
{
327
333
options . ConfigureApplication = configure ;
@@ -339,12 +345,14 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
339
345
UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
340
346
341
347
// Clear the startup type
342
- _startupObject = configure ;
348
+ _builder . Properties [ _startupConfigName ] = configure ;
343
349
344
350
_builder . ConfigureServices ( ( context , services ) =>
345
351
{
346
- if ( object . ReferenceEquals ( _startupObject , configure ) )
352
+ if ( _builder . Properties . TryGetValue ( _startupConfigName , out var startupObject ) &&
353
+ object . ReferenceEquals ( startupObject , configure ) )
347
354
{
355
+ _builder . Properties . Remove ( _startupConfigName ) ;
348
356
services . Configure < GenericWebHostServiceOptions > ( options =>
349
357
{
350
358
var webhostBuilderContext = GetWebHostBuilderContext ( context ) ;
0 commit comments