@@ -148,10 +148,10 @@ public WebApplication Build()
148148 builder . AddInMemoryCollection ( _hostConfigurationValues ) ;
149149 } ) ;
150150
151+ // Wire up the _hostBuilder's application configuration with a ChainedConfigurationSource to the ConfigurationManager.
152+ // We use a "tracking" source to avoid creating a circular reference when copying providers in ConfigureServices.
151153 var chainedConfigSource = new TrackingChainedConfigurationSource ( Configuration ) ;
152154
153- // Wire up the application configuration by copying the already built configuration providers over to final configuration builder.
154- // We wrap the existing provider in a configuration source to avoid re-bulding the already added configuration sources.
155155 _hostBuilder . ConfigureAppConfiguration ( builder =>
156156 {
157157 builder . Add ( chainedConfigSource ) ;
@@ -203,10 +203,12 @@ public WebApplication Build()
203203 ( ( IConfigurationBuilder ) Configuration ) . Sources . Clear ( ) ;
204204 }
205205
206- // Make builder.Configuration match the final configuration. To do that, we add the additional
207- // providers in the inner _hostBuilders's Configuration to the ConfigurationManager.
206+ // Make the ConfigurationManager match the final _hostBuilder's configuration. To do that, we add the additional providers
207+ // to the inner _hostBuilders's configuration to the ConfigurationManager. We wrap the existing provider in a
208+ // configuration source to avoid rebulding or reloading the already added configuration sources.
208209 foreach ( var provider in hostBuilderProviders )
209210 {
211+ // Avoid creating a circular reference to the ConfigurationManager via the chained configuration source.
210212 if ( ! ReferenceEquals ( provider , chainedConfigSource . BuiltProvider ) )
211213 {
212214 ( ( IConfigurationBuilder ) Configuration ) . Add ( new ConfigurationProviderSource ( provider ) ) ;
0 commit comments