@@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Hosting;
2020
2121internal 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 {
0 commit comments