/// <summary> /// Adds and configures an ASP.NET Core web application. /// </summary> /// <param name="builder">The <see cref="IHostBuilder"/> to add the <see cref="IWebHostBuilder"/> to.</param> /// <param name="configure">The delegate that configures the <see cref="IWebHostBuilder"/>.</param> /// <param name="configureWebHostBuilder">The delegate that configures the <see cref="WebHostBuilderOptions"/>.</param> /// <returns>The <see cref="IHostBuilder"/>.</returns> public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action <IWebHostBuilder> configure, Action <WebHostBuilderOptions> configureWebHostBuilder) { if (configure is null) { throw new ArgumentNullException(nameof(configure)); } if (configureWebHostBuilder is null) { throw new ArgumentNullException(nameof(configureWebHostBuilder)); } // Light up custom implementations namely ConfigureHostBuilder which throws. if (builder is ISupportsConfigureWebHost supportsConfigureWebHost) { return(supportsConfigureWebHost.ConfigureWebHost(configure, configureWebHostBuilder)); } var webHostBuilderOptions = new WebHostBuilderOptions(); configureWebHostBuilder(webHostBuilderOptions); var webhostBuilder = new GenericWebHostBuilder(builder, webHostBuilderOptions); configure(webhostBuilder); builder.ConfigureServices((context, services) => services.AddHostedService <GenericWebHostService>()); return(builder); }
public GenericWebHostBuilder(IHostBuilder builder, WebHostBuilderOptions options) { _builder = builder; var configBuilder = new ConfigurationBuilder() .AddInMemoryCollection(); if (!options.SuppressEnvironmentConfiguration) { configBuilder.AddEnvironmentVariables(prefix: "ASPNETCORE_"); } _config = configBuilder.Build(); _builder.ConfigureHostConfiguration(config => { config.AddConfiguration(_config); // We do this super early but still late enough that we can process the configuration // wired up by calls to UseSetting ExecuteHostingStartups(); }); // IHostingStartup needs to be executed before any direct methods on the builder // so register these callbacks first _builder.ConfigureAppConfiguration((context, configurationBuilder) => { if (_hostingStartupWebHostBuilder != null) { var webhostContext = GetWebHostBuilderContext(context); _hostingStartupWebHostBuilder.ConfigureAppConfiguration(webhostContext, configurationBuilder); } }); _builder.ConfigureServices((context, services) => { var webhostContext = GetWebHostBuilderContext(context); var webHostOptions = (WebHostOptions)context.Properties[typeof(WebHostOptions)]; // Add the IHostingEnvironment and IApplicationLifetime from Microsoft.AspNetCore.Hosting services.AddSingleton(webhostContext.HostingEnvironment); #pragma warning disable CS0618 // Type or member is obsolete services.AddSingleton((AspNetCore.Hosting.IHostingEnvironment)webhostContext.HostingEnvironment); services.AddSingleton <IApplicationLifetime, GenericWebHostApplicationLifetime>(); #pragma warning restore CS0618 // Type or member is obsolete services.Configure <GenericWebHostServiceOptions>(options => { // Set the options options.WebHostOptions = webHostOptions; // Store and forward any startup errors options.HostingStartupExceptions = _hostingStartupErrors; }); // REVIEW: This is bad since we don't own this type. Anybody could add one of these and it would mess things up // We need to flow this differently var listener = new DiagnosticListener("Microsoft.AspNetCore"); services.TryAddSingleton <DiagnosticListener>(listener); services.TryAddSingleton <DiagnosticSource>(listener); services.TryAddSingleton <IHttpContextFactory, DefaultHttpContextFactory>(); services.TryAddScoped <IMiddlewareFactory, MiddlewareFactory>(); services.TryAddSingleton <IApplicationBuilderFactory, ApplicationBuilderFactory>(); // IMPORTANT: This needs to run *before* direct calls on the builder (like UseStartup) _hostingStartupWebHostBuilder?.ConfigureServices(webhostContext, services); // Support UseStartup(assemblyName) if (!string.IsNullOrEmpty(webHostOptions.StartupAssembly)) { try { var startupType = StartupLoader.FindStartupType(webHostOptions.StartupAssembly, webhostContext.HostingEnvironment.EnvironmentName); UseStartup(startupType, context, services); } catch (Exception ex) when(webHostOptions.CaptureStartupErrors) { var capture = ExceptionDispatchInfo.Capture(ex); services.Configure <GenericWebHostServiceOptions>(options => { options.ConfigureApplication = app => { // Throw if there was any errors initializing startup capture.Throw(); }; }); } } }); }