// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.IO; using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore { /// /// Provides convenience methods for creating instances of and with pre-configured defaults. /// public static class WebHost { /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// A delegate that handles requests to the application. /// A started that hosts the application. public static IWebHost Start(RequestDelegate app) => Start(url: null, app: app); /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// The URL the hosted application will listen on. /// A delegate that handles requests to the application. /// A started that hosts the application. public static IWebHost Start(string url, RequestDelegate app) { var startupAssemblyName = app.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name; return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName); } /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// A delegate that configures the router for handling requests to the application. /// A started that hosts the application. public static IWebHost Start(Action routeBuilder) => Start(url: null, routeBuilder: routeBuilder); /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// The URL the hosted application will listen on. /// A delegate that configures the router for handling requests to the application. /// A started that hosts the application. public static IWebHost Start(string url, Action routeBuilder) { var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name; return StartWith(url, services => services.AddRouting(), appBuilder => appBuilder.UseRouter(routeBuilder), applicationName: startupAssemblyName); } /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// The delegate that configures the . /// A started that hosts the application. public static IWebHost StartWith(Action app) => StartWith(url: null, app: app); /// /// Initializes and starts a new with pre-configured defaults. /// See for details. /// /// The URL the hosted application will listen on. /// The delegate that configures the . /// A started that hosts the application. public static IWebHost StartWith(string url, Action app) => StartWith(url: url, configureServices: null, app: app, applicationName: null); private static IWebHost StartWith(string url, Action configureServices, Action app, string applicationName) { var builder = CreateDefaultBuilder(); if (!string.IsNullOrEmpty(url)) { builder.UseUrls(url); } if (configureServices != null) { builder.ConfigureServices(configureServices); } builder.Configure(app); if (!string.IsNullOrEmpty(applicationName)) { builder.UseSetting(WebHostDefaults.ApplicationKey, applicationName); } var host = builder.Build(); host.Start(); return host; } /// /// Initializes a new instance of the class with pre-configured defaults. /// /// /// The following defaults are applied to the returned : /// use Kestrel as the web server and configure it using the application's configuration providers, /// set the to the result of , /// load from 'appsettings.json' and 'appsettings.[].json', /// load from User Secrets when is 'Development' using the entry assembly, /// load from environment variables, /// configures the to log to the console and debug output, /// enables IIS integration, /// and enables the ability for frameworks to bind their options to their default configuration sections. /// /// The initialized . public static IWebHostBuilder CreateDefaultBuilder() => CreateDefaultBuilder(args: null); /// /// Initializes a new instance of the class with pre-configured defaults. /// /// /// The following defaults are applied to the returned : /// use Kestrel as the web server and configure it using the application's configuration providers, /// set the to the result of , /// load from 'appsettings.json' and 'appsettings.[].json', /// load from User Secrets when is 'Development' using the entry assembly, /// load from environment variables, /// load from supplied command line args, /// configures the to log to the console and debug output, /// enables IIS integration, /// and enables the ability for frameworks to bind their options to their default configuration sections. /// /// The command line args. /// The initialized . public static IWebHostBuilder CreateDefaultBuilder(string[] args) { var builder = new WebHostBuilder() .UseKestrel((builderContext, options) => { options.Configure(builderContext.Configuration.GetSection("Kestrel")); }) .UseContentRoot(Directory.GetCurrentDirectory()) .ConfigureAppConfiguration((hostingContext, config) => { var env = hostingContext.HostingEnvironment; config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); if (env.IsDevelopment()) { var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); if (appAssembly != null) { config.AddUserSecrets(appAssembly, optional: true); } } config.AddEnvironmentVariables(); if (args != null) { config.AddCommandLine(args); } }) .ConfigureLogging((hostingContext, logging) => { logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); logging.AddConsole(); logging.AddDebug(); }) .ConfigureServices((hostingContext, services) => { var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); services.AddHostFiltering(options => { options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" }); }); services.AddTransient(); }) .UseIISIntegration() .UseDefaultServiceProvider((context, options) => { options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); }); if (args != null) { builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build()); } return builder; } /// /// Initializes a new instance of the class with pre-configured defaults using typed Startup. /// /// /// The following defaults are applied to the returned : /// use Kestrel as the web server and configure it using the application's configuration providers, /// set the to the result of , /// load from 'appsettings.json' and 'appsettings.[].json', /// load from User Secrets when is 'Development' using the entry assembly, /// load from environment variables, /// load from supplied command line args, /// configures the to log to the console and debug output, /// enables IIS integration, /// enables the ability for frameworks to bind their options to their default configuration sections. /// /// The type containing the startup methods for the application. /// The command line args. /// The initialized . public static IWebHostBuilder CreateDefaultBuilder(string[] args) where TStartup : class => CreateDefaultBuilder(args).UseStartup(); } }