@@ -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.StartupObject" ;
2424 private readonly object _startupKey = new object ( ) ;
2525
2626 private AggregateException ? _hostingStartupErrors ;
@@ -170,13 +170,15 @@ 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 {
181+ _builder . Properties . Remove ( _startupConfigName ) ;
180182 UseStartup ( startupType , context , services ) ;
181183 }
182184 } ) ;
@@ -193,16 +195,18 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
193195 UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
194196
195197 // Clear the startup type
196- _startupObject = startupFactory ;
198+ _builder . Properties [ _startupConfigName ] = startupFactory ;
197199
198200 _builder . ConfigureServices ( ConfigureStartup ) ;
199201
200202 [ UnconditionalSuppressMessage ( "Trimmer" , "IL2072" , Justification = "Startup type created by factory can't be determined statically." ) ]
201203 void ConfigureStartup ( HostBuilderContext context , IServiceCollection services )
202204 {
203205 // 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 ) )
205208 {
209+ _builder . Properties . Remove ( _startupConfigName ) ;
206210 var webHostBuilderContext = GetWebHostBuilderContext ( context ) ;
207211 var instance = startupFactory ( webHostBuilderContext ) ?? throw new InvalidOperationException ( "The specified factory returned null startup instance." ) ;
208212 UseStartup ( instance . GetType ( ) , context , services , instance ) ;
@@ -316,12 +320,14 @@ public IWebHostBuilder Configure(Action<IApplicationBuilder> configure)
316320 UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
317321
318322 // Clear the startup type
319- _startupObject = configure ;
323+ _builder . Properties [ _startupConfigName ] = configure ;
320324
321325 _builder . ConfigureServices ( ( context , services ) =>
322326 {
323- if ( object . ReferenceEquals ( _startupObject , configure ) )
327+ if ( _builder . Properties . TryGetValue ( _startupConfigName , out var startupObject ) &&
328+ object . ReferenceEquals ( startupObject , configure ) )
324329 {
330+ _builder . Properties . Remove ( _startupConfigName ) ;
325331 services . Configure < GenericWebHostServiceOptions > ( options =>
326332 {
327333 options . ConfigureApplication = configure ;
@@ -339,12 +345,14 @@ public IWebHostBuilder Configure(Action<WebHostBuilderContext, IApplicationBuild
339345 UseSetting ( WebHostDefaults . ApplicationKey , startupAssemblyName ) ;
340346
341347 // Clear the startup type
342- _startupObject = configure ;
348+ _builder . Properties [ _startupConfigName ] = configure ;
343349
344350 _builder . ConfigureServices ( ( context , services ) =>
345351 {
346- if ( object . ReferenceEquals ( _startupObject , configure ) )
352+ if ( _builder . Properties . TryGetValue ( _startupConfigName , out var startupObject ) &&
353+ object . ReferenceEquals ( startupObject , configure ) )
347354 {
355+ _builder . Properties . Remove ( _startupConfigName ) ;
348356 services . Configure < GenericWebHostServiceOptions > ( options =>
349357 {
350358 var webhostBuilderContext = GetWebHostBuilderContext ( context ) ;
0 commit comments