4
4
#nullable enable
5
5
6
6
using System ;
7
- using System . Collections ;
8
7
using System . Collections . Generic ;
9
8
using System . Diagnostics ;
10
9
using System . IO ;
@@ -22,7 +21,7 @@ namespace Microsoft.Extensions.Hosting
22
21
public sealed class HostApplicationBuilder
23
22
{
24
23
private readonly HostBuilderContext _hostBuilderContext ;
25
- private readonly CheckedServiceCollection _checkedServiceCollection = new ( ) ;
24
+ private readonly ServiceCollection _serviceCollection = new ( ) ;
26
25
27
26
private Func < IServiceProvider > _createServiceProvider ;
28
27
private Action < object > _configureContainer = _ => { } ;
@@ -165,7 +164,7 @@ public HostApplicationBuilder(HostApplicationBuilderSettings? settings)
165
164
/// <summary>
166
165
/// A collection of services for the application to compose. This is useful for adding user provided or framework provided services.
167
166
/// </summary>
168
- public IServiceCollection Services => _checkedServiceCollection ;
167
+ public IServiceCollection Services => _serviceCollection ;
169
168
170
169
/// <summary>
171
170
/// A collection of logging providers for the application to compose. This is useful for adding new logging providers.
@@ -224,7 +223,7 @@ public IHost Build()
224
223
_appServices = _createServiceProvider ( ) ;
225
224
226
225
// Prevent further modification of the service collection now that the provider is built.
227
- _checkedServiceCollection . IsReadOnly = true ;
226
+ _serviceCollection . MakeReadOnly ( ) ;
228
227
229
228
return HostBuilder . ResolveHost ( _appServices , diagnosticListener ) ;
230
229
}
@@ -362,69 +361,6 @@ public IHostBuilder ConfigureContainer<TContainerBuilder>(Action<HostBuilderCont
362
361
}
363
362
}
364
363
365
- private sealed class CheckedServiceCollection : IServiceCollection
366
- {
367
- private readonly IServiceCollection _services = new ServiceCollection ( ) ;
368
-
369
- public bool IsReadOnly { get ; set ; }
370
- public int Count => _services . Count ;
371
-
372
- public ServiceDescriptor this [ int index ]
373
- {
374
- get => _services [ index ] ;
375
- set
376
- {
377
- CheckReadOnly ( ) ;
378
- _services [ index ] = value ;
379
- }
380
- }
381
-
382
- public IEnumerator < ServiceDescriptor > GetEnumerator ( ) => _services . GetEnumerator ( ) ;
383
- IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
384
-
385
- public int IndexOf ( ServiceDescriptor item ) => _services . IndexOf ( item ) ;
386
- public bool Contains ( ServiceDescriptor item ) => _services . Contains ( item ) ;
387
- public void CopyTo ( ServiceDescriptor [ ] array , int arrayIndex ) => _services . CopyTo ( array , arrayIndex ) ;
388
-
389
- public void Add ( ServiceDescriptor item )
390
- {
391
- CheckReadOnly ( ) ;
392
- _services . Add ( item ) ;
393
- }
394
-
395
- public void Clear ( )
396
- {
397
- CheckReadOnly ( ) ;
398
- _services . Clear ( ) ;
399
- }
400
-
401
- public void Insert ( int index , ServiceDescriptor item )
402
- {
403
- CheckReadOnly ( ) ;
404
- _services . Insert ( index , item ) ;
405
- }
406
-
407
- public bool Remove ( ServiceDescriptor item )
408
- {
409
- CheckReadOnly ( ) ;
410
- return _services . Remove ( item ) ;
411
- }
412
-
413
- public void RemoveAt ( int index )
414
- {
415
- CheckReadOnly ( ) ;
416
- _services . RemoveAt ( index ) ;
417
- }
418
-
419
- private void CheckReadOnly ( )
420
- {
421
- if ( IsReadOnly )
422
- {
423
- throw new InvalidOperationException ( SR . ServiceCollectionModificationInvalidAfterBuild ) ;
424
- }
425
- }
426
- }
427
-
428
364
private sealed class LoggingBuilder : ILoggingBuilder
429
365
{
430
366
public LoggingBuilder ( IServiceCollection services )
0 commit comments