// 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.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; 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, /// 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 adds the developer exception page when is 'Development' /// /// 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, /// 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 adds the developer exception page when is 'Development' /// /// The command line args. /// The initialized . public static IWebHostBuilder CreateDefaultBuilder(string[] args) { var builder = new WebHostBuilder() .UseKestrel() .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(); }) .UseIISIntegration() .UseDefaultServiceProvider((context, options) => { options.ValidateScopes = context.HostingEnvironment.IsDevelopment(); }) .ConfigureServices(services => { services.AddTransient, KestrelServerOptionsSetup>(); }); return builder; } } }