// 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.Globalization; using System.Linq; using System.Threading; using Microsoft.AspNetCore.Hosting.Server; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Hosting { public static class HostingAbstractionsWebHostBuilderExtensions { private static readonly string ServerUrlsSeparator = ";"; /// /// Use the given configuration settings on the web host. /// /// The to configure. /// The containing settings to be used. /// The . public static IWebHostBuilder UseConfiguration(this IWebHostBuilder hostBuilder, IConfiguration configuration) { foreach (var setting in configuration.AsEnumerable()) { hostBuilder.UseSetting(setting.Key, setting.Value); } return hostBuilder; } /// /// Set whether startup errors should be captured in the configuration settings of the web host. /// When enabled, startup exceptions will be caught and an error page will be returned. If disabled, startup exceptions will be propagated. /// /// The to configure. /// true to use startup error page; otherwise false. /// The . public static IWebHostBuilder CaptureStartupErrors(this IWebHostBuilder hostBuilder, bool captureStartupErrors) { return hostBuilder.UseSetting(WebHostDefaults.CaptureStartupErrorsKey, captureStartupErrors ? "true" : "false"); } /// /// Specify the assembly containing the startup type to be used by the web host. /// /// The to configure. /// The name of the assembly containing the startup type. /// The . public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, string startupAssemblyName) { if (startupAssemblyName == null) { throw new ArgumentNullException(nameof(startupAssemblyName)); } return hostBuilder .UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName) .UseSetting(WebHostDefaults.StartupAssemblyKey, startupAssemblyName); } /// /// Specify the server to be used by the web host. /// /// The to configure. /// The to be used. /// The . public static IWebHostBuilder UseServer(this IWebHostBuilder hostBuilder, IServer server) { if (server == null) { throw new ArgumentNullException(nameof(server)); } return hostBuilder.ConfigureServices(services => { // It would be nicer if this was transient but we need to pass in the // factory instance directly services.AddSingleton(server); }); } /// /// Specify the environment to be used by the web host. /// /// The to configure. /// The environment to host the application in. /// The . public static IWebHostBuilder UseEnvironment(this IWebHostBuilder hostBuilder, string environment) { if (environment == null) { throw new ArgumentNullException(nameof(environment)); } return hostBuilder.UseSetting(WebHostDefaults.EnvironmentKey, environment); } /// /// Specify the content root directory to be used by the web host. /// /// The to configure. /// Path to root directory of the application. /// The . public static IWebHostBuilder UseContentRoot(this IWebHostBuilder hostBuilder, string contentRoot) { if (contentRoot == null) { throw new ArgumentNullException(nameof(contentRoot)); } return hostBuilder.UseSetting(WebHostDefaults.ContentRootKey, contentRoot); } /// /// Specify the webroot directory to be used by the web host. /// /// The to configure. /// Path to the root directory used by the web server. /// The . public static IWebHostBuilder UseWebRoot(this IWebHostBuilder hostBuilder, string webRoot) { if (webRoot == null) { throw new ArgumentNullException(nameof(webRoot)); } return hostBuilder.UseSetting(WebHostDefaults.WebRootKey, webRoot); } /// /// Specify the urls the web host will listen on. /// /// The to configure. /// The urls the hosted application will listen on. /// The . public static IWebHostBuilder UseUrls(this IWebHostBuilder hostBuilder, params string[] urls) { if (urls == null) { throw new ArgumentNullException(nameof(urls)); } return hostBuilder.UseSetting(WebHostDefaults.ServerUrlsKey, string.Join(ServerUrlsSeparator, urls)); } /// /// Indicate whether the host should listen on the URLs configured on the /// instead of those configured on the . /// /// The to configure. /// true to prefer URLs configured on the ; otherwise false. /// The . public static IWebHostBuilder PreferHostingUrls(this IWebHostBuilder hostBuilder, bool preferHostingUrls) { return hostBuilder.UseSetting(WebHostDefaults.PreferHostingUrlsKey, preferHostingUrls ? "true" : "false"); } /// /// Specify the amount of time to wait for the web host to shutdown. /// /// The to configure. /// The amount of time to wait for server shutdown. /// The . public static IWebHostBuilder UseShutdownTimeout(this IWebHostBuilder hostBuilder, TimeSpan timeout) { return hostBuilder.UseSetting(WebHostDefaults.ShutdownTimeoutKey, ((int)timeout.TotalSeconds).ToString(CultureInfo.InvariantCulture)); } /// /// Start the web host and listen on the specified urls. /// /// The to start. /// The urls the hosted application will listen on. /// The . public static IWebHost Start(this IWebHostBuilder hostBuilder, params string[] urls) { var host = hostBuilder.UseUrls(urls).Build(); host.StartAsync(CancellationToken.None).GetAwaiter().GetResult(); return host; } } }