builder.ConfigureLogging((hostingContext, logging) => { var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
// IMPORTANT: This needs to be added *before* configuration is loaded, this lets // the defaults be overridden by the configuration. if (isWindows) { // Default the EventLogLoggerProvider to warning or above logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning); }
if (configureWebHostBuilder isnull) { thrownew ArgumentNullException(nameof(configureWebHostBuilder)); }
var webHostBuilderOptions = new WebHostBuilderOptions(); configureWebHostBuilder(webHostBuilderOptions); var webhostBuilder = new GenericWebHostBuilder(builder, webHostBuilderOptions); configure(webhostBuilder); builder.ConfigureServices((context, services) => services.AddHostedService<GenericWebHostService>()); return builder; }
privatevoidBuildHostConfiguration() { var configBuilder = new ConfigurationBuilder() .AddInMemoryCollection(); // Make sure there's some default storage since there are no default providers
if (string.IsNullOrEmpty(_hostingEnvironment.ApplicationName)) { // Note GetEntryAssembly returns null for the net4x console test runner. _hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name; }
_hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath); }
namespaceMicrosoft.Extensions.Hosting { ///<summary> /// Context containing the common services on the <see cref="IHost" />. Some properties may be null until set by the <see cref="IHost" />. ///</summary> publicclassHostBuilderContext { publicHostBuilderContext(IDictionary<object, object> properties) { Properties = properties ?? thrownew System.ArgumentNullException(nameof(properties)); }
///<summary> /// The <see cref="IHostEnvironment" /> initialized by the <see cref="IHost" />. ///</summary> public IHostEnvironment HostingEnvironment { get; set; }
///<summary> /// The <see cref="IConfiguration" /> containing the merged configuration of the application and the <see cref="IHost" />. ///</summary> public IConfiguration Configuration { get; set; }
///<summary> /// A central location for sharing state between components during the host building process. ///</summary> public IDictionary<object, object> Properties { get; } } }
privatevoidCreateServiceProvider() { var services = new ServiceCollection(); #pragmawarning disable CS0618 // Type or member is obsolete services.AddSingleton<IHostingEnvironment>(_hostingEnvironment); #pragmawarning restore CS0618 // Type or member is obsolete services.AddSingleton<IHostEnvironment>(_hostingEnvironment); services.AddSingleton(_hostBuilderContext); // register configuration as factory to make it dispose with the service provider services.AddSingleton(_ => _appConfiguration); #pragmawarning disable CS0618 // Type or member is obsolete services.AddSingleton<IApplicationLifetime>(s => (IApplicationLifetime)s.GetService<IHostApplicationLifetime>()); #pragmawarning restore CS0618 // Type or member is obsolete services.AddSingleton<IHostApplicationLifetime, ApplicationLifetime>();
if (_appServices == null) { thrownew InvalidOperationException(SR.NullIServiceProvider); }
// resolve configuration explicitly once to mark it as resolved within the // service provider, ensuring it will be properly disposed with the provider _ = _appServices.GetService<IConfiguration>(); }