diff --git a/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs new file mode 100644 index 0000000000..7e62cbeac0 --- /dev/null +++ b/src/Microsoft.AspNetCore.Hosting.Abstractions/HostingAbstractionsWebHostBuilderExtensions.cs @@ -0,0 +1,177 @@ +// 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.Linq; +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 assembly containing the server to be used by the web host. + /// + /// The to configure. + /// The name of the assembly containing the server to be used. + /// The . + public static IWebHostBuilder UseServer(this IWebHostBuilder hostBuilder, string assemblyName) + { + if (assemblyName == null) + { + throw new ArgumentNullException(nameof(assemblyName)); + } + + return hostBuilder.UseSetting(WebHostDefaults.ServerKey, assemblyName); + } + + /// + /// 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)); + } + + /// + /// Start the web host and listen on the speficied 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.Start(); + return host; + } + } +} diff --git a/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs b/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs index 7d64c8aaca..7b33bebe4c 100644 --- a/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs +++ b/src/Microsoft.AspNetCore.Hosting/WebHostBuilderExtensions.cs @@ -2,48 +2,15 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Linq; using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Internal; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Hosting { public static class WebHostBuilderExtensions { - 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 startup method to be used to configure the web application. /// @@ -105,122 +72,5 @@ namespace Microsoft.AspNetCore.Hosting { return hostBuilder.UseStartup(typeof(TStartup)); } - - /// - /// 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)); - } - - /// - /// Start the web host and listen on the speficied 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.Start(); - return host; - } } -} +} \ No newline at end of file