11// Copyright (c) .NET Foundation. All rights reserved.
22// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
4+ #nullable enable
5+
46using System ;
57using System . Collections . Generic ;
68using System . Diagnostics ;
9+ using System . Diagnostics . CodeAnalysis ;
710using System . IO ;
811using System . Linq ;
912using System . Reflection ;
@@ -24,13 +27,13 @@ namespace Microsoft.AspNetCore.Hosting
2427 public class WebHostBuilder : IWebHostBuilder
2528 {
2629 private readonly HostingEnvironment _hostingEnvironment ;
27- private Action < WebHostBuilderContext , IServiceCollection > _configureServices ;
30+ private readonly IConfiguration _config ;
31+ private readonly WebHostBuilderContext _context ;
2832
29- private IConfiguration _config ;
30- private WebHostOptions _options ;
31- private WebHostBuilderContext _context ;
33+ private WebHostOptions ? _options ;
3234 private bool _webHostBuilt ;
33- private Action < WebHostBuilderContext , IConfigurationBuilder > _configureAppConfigurationBuilder ;
35+ private Action < WebHostBuilderContext , IServiceCollection > ? _configureServices ;
36+ private Action < WebHostBuilderContext , IConfigurationBuilder > ? _configureAppConfigurationBuilder ;
3437
3538 /// <summary>
3639 /// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
@@ -78,7 +81,7 @@ public string GetSetting(string key)
7881 /// <param name="key">The key of the setting to add or replace.</param>
7982 /// <param name="value">The value of the setting to add or replace.</param>
8083 /// <returns>The <see cref="IWebHostBuilder"/>.</returns>
81- public IWebHostBuilder UseSetting ( string key , string value )
84+ public IWebHostBuilder UseSetting ( string key , string ? value )
8285 {
8386 _config [ key ] = value ;
8487 return this ;
@@ -212,7 +215,8 @@ IServiceProvider GetProviderFromFactory(IServiceCollection collection)
212215 }
213216 }
214217
215- private IServiceCollection BuildCommonServices ( out AggregateException hostingStartupErrors )
218+ [ MemberNotNull ( nameof ( _options ) ) ]
219+ private IServiceCollection BuildCommonServices ( out AggregateException ? hostingStartupErrors )
216220 {
217221 hostingStartupErrors = null ;
218222
@@ -231,7 +235,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
231235
232236 foreach ( var attribute in assembly . GetCustomAttributes < HostingStartupAttribute > ( ) )
233237 {
234- var hostingStartup = ( IHostingStartup ) Activator . CreateInstance ( attribute . HostingStartupType ) ;
238+ var hostingStartup = ( IHostingStartup ) Activator . CreateInstance ( attribute . HostingStartupType ) ! ;
235239 hostingStartup . Configure ( this ) ;
236240 }
237241 }
@@ -330,8 +334,8 @@ private void AddApplicationServices(IServiceCollection services, IServiceProvide
330334 // NOTE: This code overrides original services lifetime. Instances would always be singleton in
331335 // application container.
332336 var listener = hostingServiceProvider . GetService < DiagnosticListener > ( ) ;
333- services . Replace ( ServiceDescriptor . Singleton ( typeof ( DiagnosticListener ) , listener ) ) ;
334- services . Replace ( ServiceDescriptor . Singleton ( typeof ( DiagnosticSource ) , listener ) ) ;
337+ services . Replace ( ServiceDescriptor . Singleton ( typeof ( DiagnosticListener ) , listener ! ) ) ;
338+ services . Replace ( ServiceDescriptor . Singleton ( typeof ( DiagnosticSource ) , listener ! ) ) ;
335339 }
336340
337341 private string ResolveContentRootPath ( string contentRootPath , string basePath )
0 commit comments