@@ -25,22 +25,20 @@ namespace Microsoft.AspNetCore.Hosting
25
25
public class WebHostBuilder : IWebHostBuilder
26
26
{
27
27
private readonly HostingEnvironment _hostingEnvironment ;
28
- private readonly List < Action < WebHostBuilderContext , IServiceCollection > > _configureServicesDelegates ;
28
+ private Action < WebHostBuilderContext , IServiceCollection > _configureServices ;
29
29
30
30
private IConfiguration _config ;
31
31
private WebHostOptions _options ;
32
32
private WebHostBuilderContext _context ;
33
33
private bool _webHostBuilt ;
34
- private List < Action < WebHostBuilderContext , IConfigurationBuilder > > _configureAppConfigurationBuilderDelegates ;
34
+ private Action < WebHostBuilderContext , IConfigurationBuilder > _configureAppConfigurationBuilder ;
35
35
36
36
/// <summary>
37
37
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
38
38
/// </summary>
39
39
public WebHostBuilder ( )
40
40
{
41
41
_hostingEnvironment = new HostingEnvironment ( ) ;
42
- _configureServicesDelegates = new List < Action < WebHostBuilderContext , IServiceCollection > > ( ) ;
43
- _configureAppConfigurationBuilderDelegates = new List < Action < WebHostBuilderContext , IConfigurationBuilder > > ( ) ;
44
42
45
43
_config = new ConfigurationBuilder ( )
46
44
. AddEnvironmentVariables ( prefix : "ASPNETCORE_" )
@@ -111,12 +109,7 @@ public IWebHostBuilder ConfigureServices(Action<IServiceCollection> configureSer
111
109
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
112
110
public IWebHostBuilder ConfigureServices ( Action < WebHostBuilderContext , IServiceCollection > configureServices )
113
111
{
114
- if ( configureServices == null )
115
- {
116
- throw new ArgumentNullException ( nameof ( configureServices ) ) ;
117
- }
118
-
119
- _configureServicesDelegates . Add ( configureServices ) ;
112
+ _configureServices += configureServices ;
120
113
return this ;
121
114
}
122
115
@@ -131,12 +124,7 @@ public IWebHostBuilder ConfigureServices(Action<WebHostBuilderContext, IServiceC
131
124
/// </remarks>
132
125
public IWebHostBuilder ConfigureAppConfiguration ( Action < WebHostBuilderContext , IConfigurationBuilder > configureDelegate )
133
126
{
134
- if ( configureDelegate == null )
135
- {
136
- throw new ArgumentNullException ( nameof ( configureDelegate ) ) ;
137
- }
138
-
139
- _configureAppConfigurationBuilderDelegates . Add ( configureDelegate ) ;
127
+ _configureAppConfigurationBuilder += configureDelegate ;
140
128
return this ;
141
129
}
142
130
@@ -272,10 +260,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
272
260
. SetBasePath ( _hostingEnvironment . ContentRootPath )
273
261
. AddConfiguration ( _config ) ;
274
262
275
- foreach ( var configureAppConfiguration in _configureAppConfigurationBuilderDelegates )
276
- {
277
- configureAppConfiguration ( _context , builder ) ;
278
- }
263
+ _configureAppConfigurationBuilder ? . Invoke ( _context , builder ) ;
279
264
280
265
var configuration = builder . Build ( ) ;
281
266
services . AddSingleton < IConfiguration > ( configuration ) ;
@@ -329,10 +314,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
329
314
}
330
315
}
331
316
332
- foreach ( var configureServices in _configureServicesDelegates )
333
- {
334
- configureServices ( _context , services ) ;
335
- }
317
+ _configureServices ? . Invoke ( _context , services ) ;
336
318
337
319
return services ;
338
320
}
0 commit comments