diff --git a/samples/SampleStartups/StartupBlockingOnStart.cs b/samples/SampleStartups/StartupBlockingOnStart.cs index fdbc094d1b..40d914f379 100644 --- a/samples/SampleStartups/StartupBlockingOnStart.cs +++ b/samples/SampleStartups/StartupBlockingOnStart.cs @@ -28,16 +28,14 @@ namespace SampleStartups // Entry point for the application. public static void Main(string[] args) { - var config = WebApplicationConfiguration.GetDefault(args); - - var application = new WebApplicationBuilder() - .UseConfiguration(config) + var host = new WebHostBuilder() + .UseDefaultConfiguration(args) .UseStartup() .Build(); - using (application) + using (host) { - application.Start(); + host.Start(); Console.ReadLine(); } } diff --git a/samples/SampleStartups/StartupConfigureAddresses.cs b/samples/SampleStartups/StartupConfigureAddresses.cs index 00df99135b..31a9a38a43 100644 --- a/samples/SampleStartups/StartupConfigureAddresses.cs +++ b/samples/SampleStartups/StartupConfigureAddresses.cs @@ -27,15 +27,13 @@ namespace SampleStartups // Entry point for the application. public static void Main(string[] args) { - var config = WebApplicationConfiguration.GetDefault(args); - - var application = new WebApplicationBuilder() - .UseConfiguration(config) + var host = new WebHostBuilder() + .UseDefaultConfiguration(args) .UseStartup() .UseUrls("http://localhost:5000", "http://localhost:5001") .Build(); - application.Run(); + host.Run(); } } } diff --git a/samples/SampleStartups/StartupExternallyControlled.cs b/samples/SampleStartups/StartupExternallyControlled.cs index 538e97decd..b024ede322 100644 --- a/samples/SampleStartups/StartupExternallyControlled.cs +++ b/samples/SampleStartups/StartupExternallyControlled.cs @@ -11,7 +11,7 @@ namespace SampleStartups { public class StartupExternallyControlled { - private IWebApplication _application; + private IWebHost _host; private readonly List _urls = new List(); // This method gets called by the runtime. Use this method to add services to the container. @@ -35,14 +35,14 @@ namespace SampleStartups public void Start() { - _application = new WebApplicationBuilder() + _host = new WebHostBuilder() .UseStartup() .Start(_urls.ToArray()); } public void Stop() { - _application.Dispose(); + _host.Dispose(); } public void AddUrl(string url) diff --git a/samples/SampleStartups/StartupFullControl.cs b/samples/SampleStartups/StartupFullControl.cs index 1279bfb1a0..6445a77bc4 100644 --- a/samples/SampleStartups/StartupFullControl.cs +++ b/samples/SampleStartups/StartupFullControl.cs @@ -13,7 +13,7 @@ namespace SampleStartups { public static void Main(string[] args) { - var application = new WebApplicationBuilder() + var host = new WebHostBuilder() .UseServer("Microsoft.AspNet.Server.Kestrel") // Set the server manually .UseApplicationBasePath(Directory.GetCurrentDirectory()) // Override the application base with the current directory .UseUrls("http://*:1000", "https://*:902") @@ -35,7 +35,7 @@ namespace SampleStartups }) .Build(); - application.Run(); + host.Run(); } } diff --git a/samples/SampleStartups/StartupHelloWorld.cs b/samples/SampleStartups/StartupHelloWorld.cs index 4ee08e2f16..7a76ca4563 100644 --- a/samples/SampleStartups/StartupHelloWorld.cs +++ b/samples/SampleStartups/StartupHelloWorld.cs @@ -27,14 +27,12 @@ namespace SampleStartups // Entry point for the application. public static void Main(string[] args) { - var config = WebApplicationConfiguration.GetDefault(args); - - var application = new WebApplicationBuilder() - .UseConfiguration(config) + var host = new WebHostBuilder() + .UseDefaultConfiguration(args) .UseStartup() .Build(); - application.Run(); + host.Run(); } } } diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/IHostingEnvironment.cs b/src/Microsoft.AspNet.Hosting.Abstractions/IHostingEnvironment.cs index cfc96be02f..6c9ddf464b 100644 --- a/src/Microsoft.AspNet.Hosting.Abstractions/IHostingEnvironment.cs +++ b/src/Microsoft.AspNet.Hosting.Abstractions/IHostingEnvironment.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNet.Hosting /// of the "Hosting:Environment" (on Windows) or "Hosting__Environment" (on Linux & OS X) environment variable. /// // This must be settable! - string EnvironmentName { get; set; } + string EnvironmentName { get; set; } /// /// Gets or sets the absolute path to the directory that contains the web-servable application content files. diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/IWebApplicationBuilder.cs b/src/Microsoft.AspNet.Hosting.Abstractions/IWebApplicationBuilder.cs deleted file mode 100644 index d4953e3af4..0000000000 --- a/src/Microsoft.AspNet.Hosting.Abstractions/IWebApplicationBuilder.cs +++ /dev/null @@ -1,31 +0,0 @@ -// 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.Collections.Generic; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Hosting.Server; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNet.Hosting -{ - public interface IWebApplicationBuilder - { - IWebApplication Build(); - - IDictionary Settings { get; } - - IWebApplicationBuilder UseConfiguration(IConfiguration configuration); - - IWebApplicationBuilder UseServer(IServerFactory factory); - - IWebApplicationBuilder UseStartup(Type startupType); - - IWebApplicationBuilder ConfigureServices(Action configureServices); - - IWebApplicationBuilder Configure(Action configureApplication); - - IWebApplicationBuilder UseSetting(string key, string value); - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/IWebApplication.cs b/src/Microsoft.AspNet.Hosting.Abstractions/IWebHost.cs similarity index 81% rename from src/Microsoft.AspNet.Hosting.Abstractions/IWebApplication.cs rename to src/Microsoft.AspNet.Hosting.Abstractions/IWebHost.cs index cfafc97ee5..45522b6b4c 100644 --- a/src/Microsoft.AspNet.Hosting.Abstractions/IWebApplication.cs +++ b/src/Microsoft.AspNet.Hosting.Abstractions/IWebHost.cs @@ -7,9 +7,9 @@ using Microsoft.AspNet.Http.Features; namespace Microsoft.AspNet.Hosting { /// - /// Represents a configured web application + /// Represents a configured web host /// - public interface IWebApplication : IDisposable + public interface IWebHost : IDisposable { /// /// The exposed by the configured server. @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Hosting IFeatureCollection ServerFeatures { get; } /// - /// The for the application. + /// The for the host. /// IServiceProvider Services { get; } diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/IWebHostBuilder.cs b/src/Microsoft.AspNet.Hosting.Abstractions/IWebHostBuilder.cs new file mode 100644 index 0000000000..60017adb17 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting.Abstractions/IWebHostBuilder.cs @@ -0,0 +1,73 @@ +// 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.Collections.Generic; +using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNet.Hosting +{ + /// + /// A builder for + /// + public interface IWebHostBuilder + { + /// + /// Builds an which hosts a web application. + /// + IWebHost Build(); + + /// + /// Gets the raw settings to be used by the web host. Values specified here will override + /// the configuration set by . + /// + IDictionary Settings { get; } + + /// + /// Specify the to be used by the web host. If no configuration is + /// provided to the builder, the default configuration will be used. + /// + /// The to be used. + /// The . + IWebHostBuilder UseConfiguration(IConfiguration configuration); + + /// + /// Specify the to be used by the web host. + /// + /// The to be used. + /// The . + IWebHostBuilder UseServer(IServerFactory factory); + + /// + /// Specify the startup type to be used by the web host. + /// + /// The to be used. + /// The . + IWebHostBuilder UseStartup(Type startupType); + + /// + /// Specify the delegate that is used to configure the services of the web application. + /// + /// The delegate that configures the . + /// The . + IWebHostBuilder ConfigureServices(Action configureServices); + + /// + /// Specify the startup method to be used to configure the web application. + /// + /// The delegate that configures the . + /// The . + IWebHostBuilder Configure(Action configureApplication); + + /// + /// Add or replace a setting in . + /// + /// The key of the setting to add or replace. + /// The value of the setting to add or replace. + /// The . + IWebHostBuilder UseSetting(string key, string value); + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting.Abstractions/WebApplicationDefaults.cs b/src/Microsoft.AspNet.Hosting.Abstractions/WebHostDefaults.cs similarity index 95% rename from src/Microsoft.AspNet.Hosting.Abstractions/WebApplicationDefaults.cs rename to src/Microsoft.AspNet.Hosting.Abstractions/WebHostDefaults.cs index 3efa2561ad..c87cff6476 100644 --- a/src/Microsoft.AspNet.Hosting.Abstractions/WebApplicationDefaults.cs +++ b/src/Microsoft.AspNet.Hosting.Abstractions/WebHostDefaults.cs @@ -3,7 +3,7 @@ namespace Microsoft.AspNet.Hosting { - public static class WebApplicationDefaults + public static class WebHostDefaults { public static readonly string ApplicationKey = "application"; public static readonly string DetailedErrorsKey = "detailedErrors"; diff --git a/src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationService.cs b/src/Microsoft.AspNet.Hosting.WindowsServices/WebHostService.cs similarity index 77% rename from src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationService.cs rename to src/Microsoft.AspNet.Hosting.WindowsServices/WebHostService.cs index 4cecc5705a..590f54cc63 100644 --- a/src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationService.cs +++ b/src/Microsoft.AspNet.Hosting.WindowsServices/WebHostService.cs @@ -1,7 +1,6 @@ // 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.ServiceProcess; using Microsoft.Extensions.DependencyInjection; @@ -10,25 +9,25 @@ namespace Microsoft.AspNet.Hosting.WindowsServices /// /// Provides an implementation of a Windows service that hosts ASP.NET. /// - public class WebApplicationService : ServiceBase + public class WebHostService : ServiceBase { - private IWebApplication _application; + private IWebHost _host; private bool _stopRequestedByWindows; /// - /// Creates an instance of WebApplicationService which hosts the specified web application. + /// Creates an instance of WebHostService which hosts the specified web application. /// - /// The web application to host in the Windows service. - public WebApplicationService(IWebApplication application) + /// The configured web host containing the web application to host in the Windows service. + public WebHostService(IWebHost host) { - _application = application; + _host = host; } protected sealed override void OnStart(string[] args) { OnStarting(args); - _application + _host .Services .GetRequiredService() .ApplicationStopped @@ -40,7 +39,7 @@ namespace Microsoft.AspNet.Hosting.WindowsServices } }); - _application.Start(); + _host.Start(); OnStarted(); } @@ -49,7 +48,7 @@ namespace Microsoft.AspNet.Hosting.WindowsServices { _stopRequestedByWindows = true; OnStopping(); - _application?.Dispose(); + _host?.Dispose(); OnStopped(); } diff --git a/src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationExtensions.cs b/src/Microsoft.AspNet.Hosting.WindowsServices/WebHostWindowsServiceExtensions.cs similarity index 56% rename from src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationExtensions.cs rename to src/Microsoft.AspNet.Hosting.WindowsServices/WebHostWindowsServiceExtensions.cs index 41f3e0548f..4034c5976f 100644 --- a/src/Microsoft.AspNet.Hosting.WindowsServices/WebApplicationExtensions.cs +++ b/src/Microsoft.AspNet.Hosting.WindowsServices/WebHostWindowsServiceExtensions.cs @@ -6,37 +6,37 @@ using System.ServiceProcess; namespace Microsoft.AspNet.Hosting.WindowsServices { /// - /// Extensions to + /// Extensions to /// - public static class WebApplicationExtensions + public static class WebHostWindowsServiceExtensions { /// /// Runs the specified web application inside a Windows service and blocks until the service is stopped. /// - /// An instance of the to host in the Windows service. + /// An instance of the to host in the Windows service. /// - /// This example shows how to use . + /// This example shows how to use . /// /// public class Program /// { /// public static void Main(string[] args) /// { - /// var config = WebApplicationConfiguration.GetDefault(args); + /// var config = WebHostConfiguration.GetDefault(args); /// - /// var application = new WebApplicationBuilder() + /// var host = new WebHostBuilder() /// .UseConfiguration(config) /// .Build(); /// /// // This call will block until the service is stopped. - /// application.RunAsService(); + /// host.RunAsService(); /// } /// } /// /// - public static void RunAsService(this IWebApplication application) + public static void RunAsService(this IWebHost host) { - var webApplicationService = new WebApplicationService(application); - ServiceBase.Run(webApplicationService); + var webHostService = new WebHostService(host); + ServiceBase.Run(webHostService); } } } diff --git a/src/Microsoft.AspNet.Hosting/Internal/HostingEnvironmentExtensions.cs b/src/Microsoft.AspNet.Hosting/Internal/HostingEnvironmentExtensions.cs index cf4e1303bc..36f8c9bb41 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/HostingEnvironmentExtensions.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/HostingEnvironmentExtensions.cs @@ -11,7 +11,7 @@ namespace Microsoft.AspNet.Hosting.Internal { public static class HostingEnvironmentExtensions { - public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, WebApplicationOptions options, IConfiguration configuration) + public static void Initialize(this IHostingEnvironment hostingEnvironment, string applicationBasePath, WebHostOptions options, IConfiguration configuration) { if (options == null) { diff --git a/src/Microsoft.AspNet.Hosting/Internal/WebApplication.cs b/src/Microsoft.AspNet.Hosting/Internal/WebHost.cs similarity index 96% rename from src/Microsoft.AspNet.Hosting/Internal/WebApplication.cs rename to src/Microsoft.AspNet.Hosting/Internal/WebHost.cs index 066ec19253..d68d6604b7 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/WebApplication.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/WebHost.cs @@ -19,17 +19,17 @@ using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNet.Hosting.Internal { - public class WebApplication : IWebApplication + public class WebHost : IWebHost { private readonly IServiceCollection _applicationServiceCollection; private readonly IStartupLoader _startupLoader; private readonly ApplicationLifetime _applicationLifetime; - private readonly WebApplicationOptions _options; + private readonly WebHostOptions _options; private readonly IConfiguration _config; private IServiceProvider _applicationServices; private RequestDelegate _application; - private ILogger _logger; + private ILogger _logger; // Only one of these should be set internal string StartupAssemblyName { get; set; } @@ -41,10 +41,10 @@ namespace Microsoft.AspNet.Hosting.Internal internal string ServerFactoryLocation { get; set; } private IServer Server { get; set; } - public WebApplication( + public WebHost( IServiceCollection appServices, IStartupLoader startupLoader, - WebApplicationOptions options, + WebHostOptions options, IConfiguration config) { if (appServices == null) @@ -99,7 +99,7 @@ namespace Microsoft.AspNet.Hosting.Internal { Initialize(); - _logger = _applicationServices.GetRequiredService>(); + _logger = _applicationServices.GetRequiredService>(); var diagnosticSource = _applicationServices.GetRequiredService(); var httpContextFactory = _applicationServices.GetRequiredService(); @@ -183,7 +183,7 @@ namespace Microsoft.AspNet.Hosting.Internal // Write errors to standard out so they can be retrieved when not in development mode. Console.Out.WriteLine("Application startup exception: " + ex.ToString()); - var logger = _applicationServices.GetRequiredService>(); + var logger = _applicationServices.GetRequiredService>(); logger.ApplicationError(ex); // Generate an HTML error page. diff --git a/src/Microsoft.AspNet.Hosting/Internal/WebApplicationOptions.cs b/src/Microsoft.AspNet.Hosting/Internal/WebHostOptions.cs similarity index 59% rename from src/Microsoft.AspNet.Hosting/Internal/WebApplicationOptions.cs rename to src/Microsoft.AspNet.Hosting/Internal/WebHostOptions.cs index dd4ef59750..a5e6ffeec3 100644 --- a/src/Microsoft.AspNet.Hosting/Internal/WebApplicationOptions.cs +++ b/src/Microsoft.AspNet.Hosting/Internal/WebHostOptions.cs @@ -6,28 +6,28 @@ using Microsoft.Extensions.Configuration; namespace Microsoft.AspNet.Hosting.Internal { - public class WebApplicationOptions + public class WebHostOptions { private const string OldEnvironmentKey = "ENV"; - public WebApplicationOptions() + public WebHostOptions() { } - public WebApplicationOptions(IConfiguration configuration) + public WebHostOptions(IConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } - Application = configuration[WebApplicationDefaults.ApplicationKey]; - DetailedErrors = ParseBool(configuration, WebApplicationDefaults.DetailedErrorsKey); - CaptureStartupErrors = ParseBool(configuration, WebApplicationDefaults.CaptureStartupErrorsKey); - Environment = configuration[WebApplicationDefaults.EnvironmentKey] ?? configuration[OldEnvironmentKey]; - ServerFactoryLocation = configuration[WebApplicationDefaults.ServerKey]; - WebRoot = configuration[WebApplicationDefaults.WebRootKey]; - ApplicationBasePath = configuration[WebApplicationDefaults.ApplicationBaseKey]; + Application = configuration[WebHostDefaults.ApplicationKey]; + DetailedErrors = ParseBool(configuration, WebHostDefaults.DetailedErrorsKey); + CaptureStartupErrors = ParseBool(configuration, WebHostDefaults.CaptureStartupErrorsKey); + Environment = configuration[WebHostDefaults.EnvironmentKey] ?? configuration[OldEnvironmentKey]; + ServerFactoryLocation = configuration[WebHostDefaults.ServerKey]; + WebRoot = configuration[WebHostDefaults.WebRootKey]; + ApplicationBasePath = configuration[WebHostDefaults.ApplicationBaseKey]; } public string Application { get; set; } diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs b/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs deleted file mode 100644 index abc9da77a3..0000000000 --- a/src/Microsoft.AspNet.Hosting/WebApplicationBuilderExtensions.cs +++ /dev/null @@ -1,181 +0,0 @@ -// 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.Threading; -using Microsoft.AspNet.Hosting.Server; -using Microsoft.AspNet.Server.Features; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.AspNet.Hosting -{ - public static class WebApplicationBuilderExtensions - { - private static readonly string ServerUrlsSeparator = ";"; - - public static IWebApplicationBuilder UseCaptureStartupErrors(this IWebApplicationBuilder applicationBuilder, bool captureStartupError) - { - return applicationBuilder.UseSetting(WebApplicationDefaults.CaptureStartupErrorsKey, captureStartupError ? "true" : "false"); - } - - public static IWebApplicationBuilder UseStartup(this IWebApplicationBuilder applicationBuilder) where TStartup : class - { - return applicationBuilder.UseStartup(typeof(TStartup)); - } - - public static IWebApplicationBuilder UseServer(this IWebApplicationBuilder applicationBuilder, string assemblyName) - { - if (assemblyName == null) - { - throw new ArgumentNullException(nameof(assemblyName)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.ServerKey, assemblyName); - } - - public static IWebApplicationBuilder UseServer(this IWebApplicationBuilder applicationBuilder, IServer server) - { - if (server == null) - { - throw new ArgumentNullException(nameof(server)); - } - - return applicationBuilder.UseServer(new ServerFactory(server)); - } - - public static IWebApplicationBuilder UseApplicationBasePath(this IWebApplicationBuilder applicationBuilder, string applicationBasePath) - { - if (applicationBasePath == null) - { - throw new ArgumentNullException(nameof(applicationBasePath)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.ApplicationBaseKey, applicationBasePath); - } - - public static IWebApplicationBuilder UseEnvironment(this IWebApplicationBuilder applicationBuilder, string environment) - { - if (environment == null) - { - throw new ArgumentNullException(nameof(environment)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.EnvironmentKey, environment); - } - - public static IWebApplicationBuilder UseWebRoot(this IWebApplicationBuilder applicationBuilder, string webRoot) - { - if (webRoot == null) - { - throw new ArgumentNullException(nameof(webRoot)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.WebRootKey, webRoot); - } - - public static IWebApplicationBuilder UseUrls(this IWebApplicationBuilder applicationBuilder, params string[] urls) - { - if (urls == null) - { - throw new ArgumentNullException(nameof(urls)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.ServerUrlsKey, string.Join(ServerUrlsSeparator, urls)); - } - - public static IWebApplicationBuilder UseStartup(this IWebApplicationBuilder applicationBuilder, string startupAssemblyName) - { - if (startupAssemblyName == null) - { - throw new ArgumentNullException(nameof(startupAssemblyName)); - } - - return applicationBuilder.UseSetting(WebApplicationDefaults.ApplicationKey, startupAssemblyName); - } - - public static IWebApplication Start(this IWebApplicationBuilder applicationBuilder, params string[] urls) - { - var application = applicationBuilder.UseUrls(urls).Build(); - application.Start(); - return application; - } - - /// - /// Runs a web application and block the calling thread until host shutdown. - /// - /// - public static void Run(this IWebApplication application) - { - using (var cts = new CancellationTokenSource()) - { - Console.CancelKeyPress += (sender, eventArgs) => - { - cts.Cancel(); - - // Don't terminate the process immediately, wait for the Main thread to exit gracefully. - eventArgs.Cancel = true; - }; - - application.Run(cts.Token, "Application started. Press Ctrl+C to shut down."); - } - } - - /// - /// Runs a web application and block the calling thread until token is triggered or shutdown is triggered - /// - /// - /// The token to trigger shutdown - public static void Run(this IWebApplication application, CancellationToken token) - { - application.Run(token, shutdownMessage: null); - } - - private static void Run(this IWebApplication application, CancellationToken token, string shutdownMessage) - { - using (application) - { - application.Start(); - - var hostingEnvironment = application.Services.GetService(); - var applicationLifetime = application.Services.GetService(); - - Console.WriteLine("Hosting environment: " + hostingEnvironment.EnvironmentName); - - var serverAddresses = application.ServerFeatures.Get()?.Addresses; - if (serverAddresses != null) - { - foreach (var address in serverAddresses) - { - Console.WriteLine("Now listening on: " + address); - } - } - - if (!string.IsNullOrEmpty(shutdownMessage)) - { - Console.WriteLine(shutdownMessage); - } - - token.Register(state => - { - ((IApplicationLifetime)state).StopApplication(); - }, - applicationLifetime); - - applicationLifetime.ApplicationStopping.WaitHandle.WaitOne(); - } - } - - private class ServerFactory : IServerFactory - { - private readonly IServer _server; - - public ServerFactory(IServer server) - { - _server = server; - } - - public IServer CreateServer(IConfiguration configuration) => _server; - } - } -} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs similarity index 61% rename from src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs rename to src/Microsoft.AspNet.Hosting/WebHostBuilder.cs index 4ec45e9c61..0f385a8ac6 100644 --- a/src/Microsoft.AspNet.Hosting/WebApplicationBuilder.cs +++ b/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs @@ -20,13 +20,16 @@ using Microsoft.Extensions.PlatformAbstractions; namespace Microsoft.AspNet.Hosting { - public class WebApplicationBuilder : IWebApplicationBuilder + /// + /// A builder for + /// + public class WebHostBuilder : IWebHostBuilder { private readonly IHostingEnvironment _hostingEnvironment; private readonly ILoggerFactory _loggerFactory; private IConfiguration _config; - private WebApplicationOptions _options; + private WebHostOptions _options; private Action _configureServices; @@ -39,12 +42,16 @@ namespace Microsoft.AspNet.Hosting private IDictionary _settings = new Dictionary(StringComparer.OrdinalIgnoreCase); - public WebApplicationBuilder() + public WebHostBuilder() { _hostingEnvironment = new HostingEnvironment(); _loggerFactory = new LoggerFactory(); } + /// + /// Gets the raw settings to be used by the web host. Values specified here will override + /// the configuration set by . + /// public IDictionary Settings { get @@ -53,19 +60,36 @@ namespace Microsoft.AspNet.Hosting } } - public IWebApplicationBuilder UseSetting(string key, string value) + /// + /// Add or replace a setting in . + /// + /// The key of the setting to add or replace. + /// The value of the setting to add or replace. + /// The . + public IWebHostBuilder UseSetting(string key, string value) { _settings[key] = value; return this; } - public IWebApplicationBuilder UseConfiguration(IConfiguration configuration) + /// + /// Specify the to be used by the web host. If no configuration is + /// provided to the builder, the default configuration will be used. + /// + /// The to be used. + /// The . + public IWebHostBuilder UseConfiguration(IConfiguration configuration) { _config = configuration; return this; } - public IWebApplicationBuilder UseServer(IServerFactory factory) + /// + /// Specify the to be used by the web host. + /// + /// The to be used. + /// The . + public IWebHostBuilder UseServer(IServerFactory factory) { if (factory == null) { @@ -76,7 +100,12 @@ namespace Microsoft.AspNet.Hosting return this; } - public IWebApplicationBuilder UseStartup(Type startupType) + /// + /// Specify the startup type to be used by the web host. + /// + /// The to be used. + /// The . + public IWebHostBuilder UseStartup(Type startupType) { if (startupType == null) { @@ -87,13 +116,23 @@ namespace Microsoft.AspNet.Hosting return this; } - public IWebApplicationBuilder ConfigureServices(Action configureServices) + /// + /// Specify the delegate that is used to configure the services of the web application. + /// + /// The delegate that configures the . + /// The . + public IWebHostBuilder ConfigureServices(Action configureServices) { _configureServices = configureServices; return this; } - public IWebApplicationBuilder Configure(Action configureApp) + /// + /// Specify the startup method to be used to configure the web application. + /// + /// The delegate that configures the . + /// The . + public IWebHostBuilder Configure(Action configureApp) { if (configureApp == null) { @@ -104,13 +143,21 @@ namespace Microsoft.AspNet.Hosting return this; } - public IWebApplicationBuilder ConfigureLogging(Action configureLogging) + /// + /// Configure the provided which will be available as a hosting service. + /// + /// The delegate that configures the . + /// The . + public IWebHostBuilder ConfigureLogging(Action configureLogging) { configureLogging(_loggerFactory); return this; } - public IWebApplication Build() + /// + /// Builds the required services and an which hosts a web application. + /// + public IWebHost Build() { var hostingServices = BuildHostingServices(); @@ -122,26 +169,26 @@ namespace Microsoft.AspNet.Hosting // Initialize the hosting environment _hostingEnvironment.Initialize(appEnvironment.ApplicationBasePath, _options, _config); - var application = new WebApplication(hostingServices, startupLoader, _options, _config); + var host = new WebHost(hostingServices, startupLoader, _options, _config); // Only one of these should be set, but they are used in priority - application.ServerFactory = _serverFactory; - application.ServerFactoryLocation = _options.ServerFactoryLocation; + host.ServerFactory = _serverFactory; + host.ServerFactoryLocation = _options.ServerFactoryLocation; // Only one of these should be set, but they are used in priority - application.Startup = _startup; - application.StartupType = _startupType; - application.StartupAssemblyName = _options.Application; + host.Startup = _startup; + host.StartupType = _startupType; + host.StartupAssemblyName = _options.Application; - application.Initialize(); + host.Initialize(); - return application; + return host; } private IServiceCollection BuildHostingServices() { // Apply the configuration settings - var configuration = _config ?? WebApplicationConfiguration.GetDefault(); + var configuration = _config ?? WebHostConfiguration.GetDefault(); var mergedConfiguration = new ConfigurationBuilder() .Add(new IncludedConfigurationProvider(configuration)) @@ -149,7 +196,7 @@ namespace Microsoft.AspNet.Hosting .Build(); _config = mergedConfiguration; - _options = new WebApplicationOptions(_config); + _options = new WebHostOptions(_config); var services = new ServiceCollection(); services.AddSingleton(_hostingEnvironment); diff --git a/src/Microsoft.AspNet.Hosting/WebHostBuilderExtensions.cs b/src/Microsoft.AspNet.Hosting/WebHostBuilderExtensions.cs new file mode 100644 index 0000000000..db10bdd6d1 --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/WebHostBuilderExtensions.cs @@ -0,0 +1,123 @@ +// 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 Microsoft.AspNet.Hosting.Server; +using Microsoft.Extensions.Configuration; + +namespace Microsoft.AspNet.Hosting +{ + public static class WebHostBuilderExtensions + { + private static readonly string ServerUrlsSeparator = ";"; + + public static IWebHostBuilder UseDefaultConfiguration(this IWebHostBuilder builder) + { + return builder.UseDefaultConfiguration(args: null); + } + + public static IWebHostBuilder UseDefaultConfiguration(this IWebHostBuilder builder, string[] args) + { + return builder.UseConfiguration(WebHostConfiguration.GetDefault(args)); + } + + public static IWebHostBuilder UseCaptureStartupErrors(this IWebHostBuilder hostBuilder, bool captureStartupError) + { + return hostBuilder.UseSetting(WebHostDefaults.CaptureStartupErrorsKey, captureStartupError ? "true" : "false"); + } + + public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder) where TStartup : class + { + return hostBuilder.UseStartup(typeof(TStartup)); + } + + public static IWebHostBuilder UseServer(this IWebHostBuilder hostBuilder, string assemblyName) + { + if (assemblyName == null) + { + throw new ArgumentNullException(nameof(assemblyName)); + } + + return hostBuilder.UseSetting(WebHostDefaults.ServerKey, assemblyName); + } + + public static IWebHostBuilder UseServer(this IWebHostBuilder hostBuilder, IServer server) + { + if (server == null) + { + throw new ArgumentNullException(nameof(server)); + } + + return hostBuilder.UseServer(new ServerFactory(server)); + } + + public static IWebHostBuilder UseApplicationBasePath(this IWebHostBuilder hostBuilder, string applicationBasePath) + { + if (applicationBasePath == null) + { + throw new ArgumentNullException(nameof(applicationBasePath)); + } + + return hostBuilder.UseSetting(WebHostDefaults.ApplicationBaseKey, applicationBasePath); + } + + public static IWebHostBuilder UseEnvironment(this IWebHostBuilder hostBuilder, string environment) + { + if (environment == null) + { + throw new ArgumentNullException(nameof(environment)); + } + + return hostBuilder.UseSetting(WebHostDefaults.EnvironmentKey, environment); + } + + public static IWebHostBuilder UseWebRoot(this IWebHostBuilder hostBuilder, string webRoot) + { + if (webRoot == null) + { + throw new ArgumentNullException(nameof(webRoot)); + } + + return hostBuilder.UseSetting(WebHostDefaults.WebRootKey, webRoot); + } + + 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)); + } + + public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder, string startupAssemblyName) + { + if (startupAssemblyName == null) + { + throw new ArgumentNullException(nameof(startupAssemblyName)); + } + + return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName); + } + + public static IWebHost Start(this IWebHostBuilder hostBuilder, params string[] urls) + { + var host = hostBuilder.UseUrls(urls).Build(); + host.Start(); + return host; + } + + private class ServerFactory : IServerFactory + { + private readonly IServer _server; + + public ServerFactory(IServer server) + { + _server = server; + } + + public IServer CreateServer(IConfiguration configuration) => _server; + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs b/src/Microsoft.AspNet.Hosting/WebHostConfiguration.cs similarity index 78% rename from src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs rename to src/Microsoft.AspNet.Hosting/WebHostConfiguration.cs index e08549b8c8..76d539117a 100644 --- a/src/Microsoft.AspNet.Hosting/WebApplicationConfiguration.cs +++ b/src/Microsoft.AspNet.Hosting/WebHostConfiguration.cs @@ -6,7 +6,7 @@ using Microsoft.Extensions.Configuration; namespace Microsoft.AspNet.Hosting { - public class WebApplicationConfiguration + public class WebHostConfiguration { public static IConfiguration GetDefault() { @@ -17,16 +17,16 @@ namespace Microsoft.AspNet.Hosting { var defaultSettings = new Dictionary { - { WebApplicationDefaults.CaptureStartupErrorsKey, "true" } + { WebHostDefaults.CaptureStartupErrorsKey, "true" } }; // We are adding all environment variables first and then adding the ASPNET_ ones // with the prefix removed to unify with the command line and config file formats var configBuilder = new ConfigurationBuilder() .AddInMemoryCollection(defaultSettings) - .AddJsonFile(WebApplicationDefaults.HostingJsonFile, optional: true) + .AddJsonFile(WebHostDefaults.HostingJsonFile, optional: true) .AddEnvironmentVariables() - .AddEnvironmentVariables(prefix: WebApplicationDefaults.EnvironmentVariablesPrefix); + .AddEnvironmentVariables(prefix: WebHostDefaults.EnvironmentVariablesPrefix); if (args != null) { diff --git a/src/Microsoft.AspNet.Hosting/WebHostExtensions.cs b/src/Microsoft.AspNet.Hosting/WebHostExtensions.cs new file mode 100644 index 0000000000..0e2f6480fe --- /dev/null +++ b/src/Microsoft.AspNet.Hosting/WebHostExtensions.cs @@ -0,0 +1,78 @@ +// 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.Threading; +using Microsoft.AspNet.Server.Features; +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.AspNet.Hosting +{ + public static class WebHostExtensions + { + /// + /// Runs a web application and block the calling thread until host shutdown. + /// + /// + public static void Run(this IWebHost host) + { + using (var cts = new CancellationTokenSource()) + { + Console.CancelKeyPress += (sender, eventArgs) => + { + cts.Cancel(); + + // Don't terminate the process immediately, wait for the Main thread to exit gracefully. + eventArgs.Cancel = true; + }; + + host.Run(cts.Token, "Application started. Press Ctrl+C to shut down."); + } + } + + /// + /// Runs a web application and block the calling thread until token is triggered or shutdown is triggered + /// + /// + /// The token to trigger shutdown + public static void Run(this IWebHost host, CancellationToken token) + { + host.Run(token, shutdownMessage: null); + } + + private static void Run(this IWebHost host, CancellationToken token, string shutdownMessage) + { + using (host) + { + host.Start(); + + var hostingEnvironment = host.Services.GetService(); + var applicationLifetime = host.Services.GetService(); + + Console.WriteLine("Hosting environment: " + hostingEnvironment.EnvironmentName); + + var serverAddresses = host.ServerFeatures.Get()?.Addresses; + if (serverAddresses != null) + { + foreach (var address in serverAddresses) + { + Console.WriteLine("Now listening on: " + address); + } + } + + if (!string.IsNullOrEmpty(shutdownMessage)) + { + Console.WriteLine(shutdownMessage); + } + + token.Register(state => + { + ((IApplicationLifetime)state).StopApplication(); + }, + applicationLifetime); + + applicationLifetime.ApplicationStopping.WaitHandle.WaitOne(); + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.TestHost/TestServer.cs b/src/Microsoft.AspNet.TestHost/TestServer.cs index 57a1cefd7c..1e85bf96cb 100644 --- a/src/Microsoft.AspNet.TestHost/TestServer.cs +++ b/src/Microsoft.AspNet.TestHost/TestServer.cs @@ -16,29 +16,29 @@ namespace Microsoft.AspNet.TestHost { private const string DefaultEnvironmentName = "Development"; private const string ServerName = nameof(TestServer); - private IWebApplication _appInstance; + private IWebHost _hostInstance; private bool _disposed = false; private IHttpApplication _application; - public TestServer(IWebApplicationBuilder builder) + public TestServer(IWebHostBuilder builder) { - if (!builder.Settings.ContainsKey(WebApplicationDefaults.CaptureStartupErrorsKey)) + if (!builder.Settings.ContainsKey(WebHostDefaults.CaptureStartupErrorsKey)) { builder.UseCaptureStartupErrors(false); } - var application = builder.UseServer(this).Build(); - application.Start(); - _appInstance = application; + var host = builder.UseServer(this).Build(); + host.Start(); + _hostInstance = host; } public Uri BaseAddress { get; set; } = new Uri("http://localhost/"); - public IWebApplication Application + public IWebHost Host { get { - return _appInstance; + return _hostInstance; } } @@ -76,7 +76,7 @@ namespace Microsoft.AspNet.TestHost if (!_disposed) { _disposed = true; - _appInstance.Dispose(); + _hostInstance.Dispose(); } } diff --git a/test/Microsoft.AspNet.Hosting.Tests/HostingEnvironmentExtensionsTests.cs b/test/Microsoft.AspNet.Hosting.Tests/HostingEnvironmentExtensionsTests.cs index dd60adc4b5..60a413acf5 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/HostingEnvironmentExtensionsTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/HostingEnvironmentExtensionsTests.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Hosting.Tests { var env = new HostingEnvironment(); - env.Initialize(".", new WebApplicationOptions() {WebRoot = "testroot"}, null); + env.Initialize(".", new WebHostOptions() {WebRoot = "testroot"}, null); Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath); Assert.IsAssignableFrom(env.WebRootFileProvider); @@ -28,7 +28,7 @@ namespace Microsoft.AspNet.Hosting.Tests { var env = new HostingEnvironment(); - env.Initialize("testroot", new WebApplicationOptions(), null); + env.Initialize("testroot", new WebHostOptions(), null); Assert.Equal(Path.GetFullPath(Path.Combine("testroot","wwwroot")), env.WebRootPath); Assert.IsAssignableFrom(env.WebRootFileProvider); @@ -39,7 +39,7 @@ namespace Microsoft.AspNet.Hosting.Tests { var env = new HostingEnvironment(); - env.Initialize(Path.Combine("testroot", "wwwroot"), new WebApplicationOptions(), null); + env.Initialize(Path.Combine("testroot", "wwwroot"), new WebHostOptions(), null); Assert.Null(env.WebRootPath); Assert.IsAssignableFrom(env.WebRootFileProvider); @@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Hosting.Tests var config = new ConfigurationBuilder().Build(); var env = new HostingEnvironment(); - env.Initialize(".", new WebApplicationOptions(), config); + env.Initialize(".", new WebHostOptions(), config); Assert.Same(config, env.Configuration); } @@ -62,7 +62,7 @@ namespace Microsoft.AspNet.Hosting.Tests var env = new HostingEnvironment(); env.EnvironmentName = "SomeName"; - env.Initialize(".", new WebApplicationOptions() { Environment = "NewName" }, null); + env.Initialize(".", new WebHostOptions() { Environment = "NewName" }, null); Assert.Equal("NewName", env.EnvironmentName); } @@ -72,7 +72,7 @@ namespace Microsoft.AspNet.Hosting.Tests { var env = new HostingEnvironment(); - env.Initialize(".", new WebApplicationOptions(), null); + env.Initialize(".", new WebHostOptions(), null); Assert.Throws(() => env.MapPath("file.txt")); } diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs similarity index 71% rename from test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs rename to test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs index d316a3b460..01c5346c66 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationBuilderTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostBuilderTests.cs @@ -18,27 +18,27 @@ using Xunit; namespace Microsoft.AspNet.Hosting { - public class WebApplicationBuilderTests + public class WebHostBuilderTests { [Fact] public void Build_honors_UseStartup_with_string() { - var builder = CreateWebApplicationBuilder().UseServer(new TestServer()); + var builder = CreateWebHostBuilder().UseServer(new TestServer()); - var application = (WebApplication)builder.UseStartup("MyStartupAssembly").Build(); + var host = (WebHost)builder.UseStartup("MyStartupAssembly").Build(); - Assert.Equal("MyStartupAssembly", application.StartupAssemblyName); + Assert.Equal("MyStartupAssembly", host.StartupAssemblyName); } [Fact] public async Task StartupMissing_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup("MissingStartupAssembly").Build(); - using (application) + var host = builder.UseServer(server).UseStartup("MissingStartupAssembly").Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "MissingStartupAssembly"); } } @@ -46,12 +46,12 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task StartupStaticCtorThrows_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "Exception from static constructor"); } } @@ -59,12 +59,12 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task StartupCtorThrows_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "Exception from constructor"); } } @@ -72,12 +72,12 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task StartupCtorThrows_TypeLoadException() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "Message from the LoaderException"); } } @@ -85,13 +85,13 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task IApplicationLifetimeRegisteredEvenWhenStartupCtorThrows_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); - var service = application.Services.GetServices(); + host.Start(); + var service = host.Services.GetServices(); Assert.NotNull(service); await AssertResponseContains(server.RequestDelegate, "Exception from constructor"); } @@ -100,12 +100,12 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task StartupConfigureServicesThrows_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "Exception from ConfigureServices"); } } @@ -113,12 +113,12 @@ namespace Microsoft.AspNet.Hosting [Fact] public async Task StartupConfigureThrows_Fallback() { - var builder = CreateWebApplicationBuilder(); + var builder = CreateWebHostBuilder(); var server = new TestServer(); - var application = builder.UseServer(server).UseStartup().Build(); - using (application) + var host = builder.UseServer(server).UseStartup().Build(); + using (host) { - application.Start(); + host.Start(); await AssertResponseContains(server.RequestDelegate, "Exception from Configure"); } } @@ -126,23 +126,23 @@ namespace Microsoft.AspNet.Hosting [Fact] public void CaptureStartupErrorsByDefault() { - var applicationBuilder = new WebApplicationBuilder() + var hostBuilder = new WebHostBuilder() .UseServer(new TestServer()) .UseStartup(); // This should not throw - applicationBuilder.Build(); + hostBuilder.Build(); } [Fact] public void UseCaptureStartupErrorsHonored() { - var applicationBuilder = new WebApplicationBuilder() + var hostBuilder = new WebHostBuilder() .UseCaptureStartupErrors(false) .UseServer(new TestServer()) .UseStartup(); - var exception = Assert.Throws(() => applicationBuilder.Build()); + var exception = Assert.Throws(() => hostBuilder.Build()); Assert.Equal("A public method named 'ConfigureProduction' or 'Configure' could not be found in the 'Microsoft.AspNet.Hosting.Fakes.StartupBoom' type.", exception.Message); } @@ -158,14 +158,14 @@ namespace Microsoft.AspNet.Hosting var config = builder.Build(); var expected = "MY_TEST_ENVIRONMENT"; - var application = new WebApplicationBuilder() + var host = new WebHostBuilder() .UseConfiguration(config) .UseEnvironment(expected) .UseServer(new TestServer()) .UseStartup("Microsoft.AspNet.Hosting.Tests") .Build(); - Assert.Equal(expected, application.Services.GetService().EnvironmentName); + Assert.Equal(expected, host.Services.GetService().EnvironmentName); } [Fact] @@ -180,14 +180,14 @@ namespace Microsoft.AspNet.Hosting var config = builder.Build(); var expected = "MY_TEST_ENVIRONMENT"; - var application = new WebApplicationBuilder() + var host = new WebHostBuilder() .UseConfiguration(config) .UseEnvironment(expected) .UseServer(new TestServer()) .UseStartup("Microsoft.AspNet.Hosting.Tests") .Build(); - application.Dispose(); + host.Dispose(); } [Fact] @@ -201,17 +201,17 @@ namespace Microsoft.AspNet.Hosting .AddInMemoryCollection(vals); var config = builder.Build(); - var application = new WebApplicationBuilder() + var host = new WebHostBuilder() .UseConfiguration(config) .UseApplicationBasePath("/foo/bar") .UseServer(new TestServer()) .UseStartup("Microsoft.AspNet.Hosting.Tests") .Build(); - Assert.Equal("/foo/bar", application.Services.GetService().ApplicationBasePath); + Assert.Equal("/foo/bar", host.Services.GetService().ApplicationBasePath); } - private IWebApplicationBuilder CreateWebApplicationBuilder() + private IWebHostBuilder CreateWebHostBuilder() { var vals = new Dictionary { @@ -221,7 +221,7 @@ namespace Microsoft.AspNet.Hosting var builder = new ConfigurationBuilder() .AddInMemoryCollection(vals); var config = builder.Build(); - return new WebApplicationBuilder().UseConfiguration(config); + return new WebHostBuilder().UseConfiguration(config); } private async Task AssertResponseContains(RequestDelegate app, string expectedText) diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostConfigurationsTests.cs similarity index 78% rename from test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs rename to test/Microsoft.AspNet.Hosting.Tests/WebHostConfigurationsTests.cs index 8c27409f1c..b88eb51dc9 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationConfigurationsTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostConfigurationsTests.cs @@ -8,12 +8,12 @@ using Xunit; namespace Microsoft.AspNet.Hosting.Tests { - public class WebApplicationConfigurationTests + public class WebHostConfigurationTests { [Fact] public void DefaultCapturesStartupErrors() { - var config = new WebApplicationOptions(WebApplicationConfiguration.GetDefault()); + var config = new WebHostOptions(WebHostConfiguration.GetDefault()); Assert.True(config.CaptureStartupErrors); } @@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Hosting.Tests { "captureStartupErrors", "true" } }; - var config = new WebApplicationOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); + var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal("wwwroot", config.WebRoot); Assert.Equal("Microsoft.AspNet.Server.Kestrel", config.ServerFactoryLocation); @@ -45,7 +45,7 @@ namespace Microsoft.AspNet.Hosting.Tests public void ReadsOldEnvKey() { var parameters = new Dictionary() { { "ENV", "Development" } }; - var config = new WebApplicationOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); + var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal("Development", config.Environment); } @@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Hosting.Tests public void AllowsNumberForDetailedErrors(string value, bool expected) { var parameters = new Dictionary() { { "detailedErrors", value } }; - var config = new WebApplicationOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); + var config = new WebHostOptions(new ConfigurationBuilder().AddInMemoryCollection(parameters).Build()); Assert.Equal(expected, config.DetailedErrors); } diff --git a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationTests.cs b/test/Microsoft.AspNet.Hosting.Tests/WebHostTests.cs similarity index 79% rename from test/Microsoft.AspNet.Hosting.Tests/WebApplicationTests.cs rename to test/Microsoft.AspNet.Hosting.Tests/WebHostTests.cs index a759535009..1526192d94 100644 --- a/test/Microsoft.AspNet.Hosting.Tests/WebApplicationTests.cs +++ b/test/Microsoft.AspNet.Hosting.Tests/WebHostTests.cs @@ -25,7 +25,7 @@ using Xunit; namespace Microsoft.AspNet.Hosting { - public class WebApplicationTests : IServerFactory, IServer + public class WebHostTests : IServerFactory, IServer { private readonly IList _startInstances = new List(); private IFeatureCollection _featuresSupportedByThisHost = NewFeatureCollection(); @@ -64,7 +64,7 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebApplicationThrowsWithNoServer() + public void WebHostThrowsWithNoServer() { var ex = Assert.Throws(() => CreateBuilder().Build().Start()); Assert.True(ex.Message.Contains("UseServer()")); @@ -87,9 +87,9 @@ namespace Microsoft.AspNet.Hosting var builder = new ConfigurationBuilder() .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).Build(); - application.Start(); - Assert.NotNull(application.Services.GetService()); + var host = CreateBuilder(config).Build(); + host.Start(); + Assert.NotNull(host.Services.GetService()); } [Fact] @@ -103,9 +103,9 @@ namespace Microsoft.AspNet.Hosting var builder = new ConfigurationBuilder() .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).Build(); - application.Start(); - Assert.NotNull(application.Services.GetService()); + var host = CreateBuilder(config).Build(); + host.Start(); + Assert.NotNull(host.Services.GetService()); } [Fact] @@ -119,10 +119,10 @@ namespace Microsoft.AspNet.Hosting var builder = new ConfigurationBuilder() .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).Build(); - application.Start(); - Assert.NotNull(application.Services.GetService()); - Assert.Equal("http://localhost:5000", application.ServerFeatures.Get().Addresses.First()); + var host = CreateBuilder(config).Build(); + host.Start(); + Assert.NotNull(host.Services.GetService()); + Assert.Equal("http://localhost:5000", host.ServerFeatures.Get().Addresses.First()); } [Fact] @@ -136,45 +136,45 @@ namespace Microsoft.AspNet.Hosting var builder = new ConfigurationBuilder() .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).Build(); - application.Start(); - var hostingEnvironment = application.Services.GetService(); + var host = CreateBuilder(config).Build(); + host.Start(); + var hostingEnvironment = host.Services.GetService(); Assert.NotNull(hostingEnvironment.Configuration); Assert.Equal("Microsoft.AspNet.Hosting.Tests", hostingEnvironment.Configuration["Server"]); } [Fact] - public void WebApplicationCanBeStarted() + public void WebHostCanBeStarted() { - var app = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .UseStartup("Microsoft.AspNet.Hosting.Tests") .Start(); - Assert.NotNull(app); + Assert.NotNull(host); Assert.Equal(1, _startInstances.Count); Assert.Equal(0, _startInstances[0].DisposeCalls); - app.Dispose(); + host.Dispose(); Assert.Equal(1, _startInstances[0].DisposeCalls); } [Fact] - public void WebApplicationShutsDownWhenTokenTriggers() + public void WebHostShutsDownWhenTokenTriggers() { - var app = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .UseStartup("Microsoft.AspNet.Hosting.Tests") .Build(); - var lifetime = app.Services.GetRequiredService(); + var lifetime = host.Services.GetRequiredService(); var cts = new CancellationTokenSource(); - Task.Run(() => app.Run(cts.Token)); + Task.Run(() => host.Run(cts.Token)); - // Wait on the app to be started + // Wait on the host to be started lifetime.ApplicationStarted.WaitHandle.WaitOne(); Assert.Equal(1, _startInstances.Count); @@ -182,16 +182,16 @@ namespace Microsoft.AspNet.Hosting cts.Cancel(); - // Wait on the app to shutdown + // Wait on the host to shutdown lifetime.ApplicationStopped.WaitHandle.WaitOne(); Assert.Equal(1, _startInstances[0].DisposeCalls); } [Fact] - public void WebApplicationDisposesServiceProvider() + public void WebHostDisposesServiceProvider() { - var application = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .ConfigureServices(s => { @@ -201,49 +201,49 @@ namespace Microsoft.AspNet.Hosting .UseStartup("Microsoft.AspNet.Hosting.Tests") .Build(); - application.Start(); + host.Start(); - var singleton = (FakeService)application.Services.GetService(); - var transient = (FakeService)application.Services.GetService(); + var singleton = (FakeService)host.Services.GetService(); + var transient = (FakeService)host.Services.GetService(); Assert.False(singleton.Disposed); Assert.False(transient.Disposed); - application.Dispose(); + host.Dispose(); Assert.True(singleton.Disposed); Assert.True(transient.Disposed); } [Fact] - public void WebApplicationNotifiesApplicationStarted() + public void WebHostNotifiesApplicationStarted() { - var application = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .Build(); - var applicationLifetime = application.Services.GetService(); + var applicationLifetime = host.Services.GetService(); Assert.False(applicationLifetime.ApplicationStarted.IsCancellationRequested); - using (application) + using (host) { - application.Start(); + host.Start(); Assert.True(applicationLifetime.ApplicationStarted.IsCancellationRequested); } } [Fact] - public void WebApplicationInjectsHostingEnvironment() + public void WebHostInjectsHostingEnvironment() { - var application = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .UseStartup("Microsoft.AspNet.Hosting.Tests") .UseEnvironment("WithHostingEnvironment") .Build(); - using (application) + using (host) { - application.Start(); - var env = application.Services.GetService(); + host.Start(); + var env = host.Services.GetService(); Assert.Equal("Changed", env.EnvironmentName); } } @@ -265,15 +265,15 @@ namespace Microsoft.AspNet.Hosting [Fact] public void CanCreateApplicationServicesWithAddedServices() { - var application = CreateBuilder().UseServer((IServerFactory)this).ConfigureServices(services => services.AddOptions()).Build(); - Assert.NotNull(application.Services.GetRequiredService>()); + var host = CreateBuilder().UseServer((IServerFactory)this).ConfigureServices(services => services.AddOptions()).Build(); + Assert.NotNull(host.Services.GetRequiredService>()); } [Fact] public void EnvDefaultsToProductionIfNoConfig() { - var application = CreateBuilder().UseServer((IServerFactory)this).Build(); - var env = application.Services.GetService(); + var host = CreateBuilder().UseServer((IServerFactory)this).Build(); + var env = host.Services.GetService(); Assert.Equal(EnvironmentName.Production, env.EnvironmentName); } @@ -282,7 +282,7 @@ namespace Microsoft.AspNet.Hosting { var vals = new Dictionary { - // Old key is actualy ASPNET_ENV but WebApplicationConfiguration expects environment + // Old key is actualy ASPNET_ENV but WebHostConfiguration expects environment // variable names stripped from ASPNET_ prefix so using just ENV here { "ENV", "Staging" } }; @@ -291,8 +291,8 @@ namespace Microsoft.AspNet.Hosting .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).UseServer((IServerFactory)this).Build(); - var env = application.Services.GetService(); + var host = CreateBuilder(config).UseServer((IServerFactory)this).Build(); + var env = host.Services.GetService(); Assert.Equal("Staging", env.EnvironmentName); } @@ -308,8 +308,8 @@ namespace Microsoft.AspNet.Hosting .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).UseServer((IServerFactory)this).Build(); - var env = application.Services.GetService(); + var host = CreateBuilder(config).UseServer((IServerFactory)this).Build(); + var env = host.Services.GetService(); Assert.Equal("Staging", env.EnvironmentName); } @@ -325,8 +325,8 @@ namespace Microsoft.AspNet.Hosting .AddInMemoryCollection(vals); var config = builder.Build(); - var application = CreateBuilder(config).UseServer((IServerFactory)this).Build(); - var env = application.Services.GetService(); + var host = CreateBuilder(config).UseServer((IServerFactory)this).Build(); + var env = host.Services.GetService(); Assert.Equal(Path.GetFullPath("testroot"), env.WebRootPath); Assert.True(env.WebRootFileProvider.GetFileInfo("TextFile.txt").Exists); } @@ -334,11 +334,11 @@ namespace Microsoft.AspNet.Hosting [Fact] public void IsEnvironment_Extension_Is_Case_Insensitive() { - var application = CreateBuilder().UseServer((IServerFactory)this).Build(); - using (application) + var host = CreateBuilder().UseServer((IServerFactory)this).Build(); + using (host) { - application.Start(); - var env = application.Services.GetRequiredService(); + host.Start(); + var env = host.Services.GetRequiredService(); Assert.True(env.IsEnvironment(EnvironmentName.Production)); Assert.True(env.IsEnvironment("producTion")); } @@ -366,7 +366,7 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebApplication_CreatesDefaultRequestIdentifierFeature_IfNotPresent() + public void WebHost_CreatesDefaultRequestIdentifierFeature_IfNotPresent() { // Arrange HttpContext httpContext = null; @@ -375,10 +375,10 @@ namespace Microsoft.AspNet.Hosting httpContext = innerHttpContext; return Task.FromResult(0); }); - var application = CreateApplication(requestDelegate); + var host = CreateHost(requestDelegate); // Act - application.Start(); + host.Start(); // Assert Assert.NotNull(httpContext); @@ -388,7 +388,7 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebApplication_DoesNot_CreateDefaultRequestIdentifierFeature_IfPresent() + public void WebHost_DoesNot_CreateDefaultRequestIdentifierFeature_IfPresent() { // Arrange HttpContext httpContext = null; @@ -399,10 +399,10 @@ namespace Microsoft.AspNet.Hosting }); var requestIdentifierFeature = new StubHttpRequestIdentifierFeature(); _featuresSupportedByThisHost[typeof(IHttpRequestIdentifierFeature)] = requestIdentifierFeature; - var application = CreateApplication(requestDelegate); + var host = CreateHost(requestDelegate); // Act - application.Start(); + host.Start(); // Assert Assert.NotNull(httpContext); @@ -410,17 +410,17 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebApplication_InvokesConfigureMethodsOnlyOnce() + public void WebHost_InvokesConfigureMethodsOnlyOnce() { - var application = CreateBuilder() + var host = CreateBuilder() .UseServer((IServerFactory)this) .UseStartup() .Build(); - using (application) + using (host) { - application.Start(); - var services = application.Services; - var services2 = application.Services; + host.Start(); + var services = host.Services; + var services2 = host.Services; Assert.Equal(1, CountStartup.ConfigureCount); Assert.Equal(1, CountStartup.ConfigureServicesCount); } @@ -443,7 +443,7 @@ namespace Microsoft.AspNet.Hosting } [Fact] - public void WebApplication_ThrowsForBadConfigureServiceSignature() + public void WebHost_ThrowsForBadConfigureServiceSignature() { var builder = CreateBuilder() .UseServer((IServerFactory)this) @@ -459,7 +459,7 @@ namespace Microsoft.AspNet.Hosting public void Configure(IApplicationBuilder app) { } } - private IWebApplication CreateApplication(RequestDelegate requestDelegate) + private IWebHost CreateHost(RequestDelegate requestDelegate) { var builder = CreateBuilder() .UseServer((IServerFactory)this) @@ -474,12 +474,12 @@ namespace Microsoft.AspNet.Hosting private void RunMapPath(string virtualPath, string expectedSuffix) { - var application = CreateBuilder().UseServer((IServerFactory)this).Build(); + var host = CreateBuilder().UseServer((IServerFactory)this).Build(); - using (application) + using (host) { - application.Start(); - var env = application.Services.GetRequiredService(); + host.Start(); + var env = host.Services.GetRequiredService(); // MapPath requires webroot to be set, we don't care // about file provider so just set it here env.WebRootPath = "."; @@ -489,9 +489,9 @@ namespace Microsoft.AspNet.Hosting } } - private IWebApplicationBuilder CreateBuilder(IConfiguration config = null) + private IWebHostBuilder CreateBuilder(IConfiguration config = null) { - return new WebApplicationBuilder().UseConfiguration(config ?? new ConfigurationBuilder().Build()).UseStartup("Microsoft.AspNet.Hosting.Tests"); + return new WebHostBuilder().UseConfiguration(config ?? new ConfigurationBuilder().Build()).UseStartup("Microsoft.AspNet.Hosting.Tests"); } public void Start(IHttpApplication application) diff --git a/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs index b439df01bd..06b570afb3 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/ClientHandlerTests.cs @@ -283,10 +283,10 @@ namespace Microsoft.AspNet.TestHost { // This logger will attempt to access information from HttpRequest once the HttpContext is created var logger = new VerifierLogger(); - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .ConfigureServices(services => { - services.AddSingleton>(logger); + services.AddSingleton>(logger); }) .Configure(app => { @@ -301,7 +301,7 @@ namespace Microsoft.AspNet.TestHost var result = await server.CreateClient().GetStringAsync("/"); } - private class VerifierLogger : ILogger + private class VerifierLogger : ILogger { public IDisposable BeginScopeImpl(object state) => new NoopDispoasble(); diff --git a/test/Microsoft.AspNet.TestHost.Tests/RequestBuilderTests.cs b/test/Microsoft.AspNet.TestHost.Tests/RequestBuilderTests.cs index a47cf1f00c..77b2e6e9b2 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/RequestBuilderTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/RequestBuilderTests.cs @@ -14,7 +14,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono)] public void AddRequestHeader() { - var builder = new WebApplicationBuilder().Configure(app => { }); + var builder = new WebHostBuilder().Configure(app => { }); var server = new TestServer(builder); server.CreateRequest("/") .AddHeader("Host", "MyHost:90") @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.TestHost [Fact] public void AddContentHeaders() { - var builder = new WebApplicationBuilder().Configure(app => { }); + var builder = new WebHostBuilder().Configure(app => { }); var server = new TestServer(builder); server.CreateRequest("/") .AddHeader("Content-Type", "Test/Value") diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs index 42a1a7752f..4312836514 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestClientTests.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNet.TestHost var expected = "GET Response"; RequestDelegate appDelegate = ctx => ctx.Response.WriteAsync(expected); - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); @@ -53,7 +53,7 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("/", ctx.Request.Path.Value); return ctx.Response.WriteAsync(expected); }; - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); @@ -76,7 +76,7 @@ namespace Microsoft.AspNet.TestHost Assert.Equal("/", ctx.Request.Path.Value); return ctx.Response.WriteAsync(expected); }; - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); @@ -94,7 +94,7 @@ namespace Microsoft.AspNet.TestHost // Arrange RequestDelegate appDelegate = ctx => ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd() + " PUT Response"); - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); @@ -113,7 +113,7 @@ namespace Microsoft.AspNet.TestHost // Arrange RequestDelegate appDelegate = async ctx => await ctx.Response.WriteAsync(new StreamReader(ctx.Request.Body).ReadToEnd() + " POST Response"); - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); @@ -154,10 +154,10 @@ namespace Microsoft.AspNet.TestHost } } }; - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .ConfigureServices(services => { - services.AddSingleton>(logger); + services.AddSingleton>(logger); }) .Configure(app => { @@ -197,7 +197,7 @@ namespace Microsoft.AspNet.TestHost } - private class VerifierLogger : ILogger + private class VerifierLogger : ILogger { public IDisposable BeginScopeImpl(object state) => new NoopDispoasble(); @@ -227,7 +227,7 @@ namespace Microsoft.AspNet.TestHost websocket.Dispose(); } }; - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(appDelegate); }); @@ -261,7 +261,7 @@ namespace Microsoft.AspNet.TestHost } } }; - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(appDelegate); }); @@ -310,7 +310,7 @@ namespace Microsoft.AspNet.TestHost }; // Act - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost:12345"); @@ -342,7 +342,7 @@ namespace Microsoft.AspNet.TestHost }; // Act - var builder = new WebApplicationBuilder().Configure(app => app.Run(appDelegate)); + var builder = new WebHostBuilder().Configure(app => app.Run(appDelegate)); var server = new TestServer(builder); var client = server.CreateClient(); var cts = new CancellationTokenSource(); diff --git a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs index 2919f940ba..d544e1bf14 100644 --- a/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs +++ b/test/Microsoft.AspNet.TestHost.Tests/TestServerTests.cs @@ -27,14 +27,14 @@ namespace Microsoft.AspNet.TestHost { // Arrange // Act & Assert (Does not throw) - new TestServer(new WebApplicationBuilder().Configure(app => { })); + new TestServer(new WebHostBuilder().Configure(app => { })); } [Fact] public void DoesNotCaptureStartupErrorsByDefault() { - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .Configure(app => { throw new InvalidOperationException(); @@ -47,7 +47,7 @@ namespace Microsoft.AspNet.TestHost [Fact] public void CaptureStartupErrorsSettingPreserved() { - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .UseCaptureStartupErrors(true) .Configure(app => { @@ -62,7 +62,7 @@ namespace Microsoft.AspNet.TestHost public void ApplicationServicesAvailableFromTestServer() { var testService = new TestService(); - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .Configure(app => { }) .ConfigureServices(services => { @@ -70,14 +70,14 @@ namespace Microsoft.AspNet.TestHost }); var server = new TestServer(builder); - Assert.Equal(testService, server.Application.Services.GetRequiredService()); + Assert.Equal(testService, server.Host.Services.GetRequiredService()); } [ConditionalFact] [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task RequestServicesAutoCreated() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -114,7 +114,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CustomServiceProviderSetsApplicationServices() { - var builder = new WebApplicationBuilder().UseStartup(); + var builder = new WebHostBuilder().UseStartup(); var server = new TestServer(builder); string result = await server.CreateClient().GetStringAsync("/path"); Assert.Equal("ApplicationServicesEqual:True", result); @@ -157,7 +157,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task ExistingRequestServicesWillNotBeReplaced() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -179,7 +179,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanSetCustomServiceProvider() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -229,7 +229,7 @@ namespace Microsoft.AspNet.TestHost public async Task ExistingServiceProviderFeatureWillNotBeReplaced() { var appServices = new ServiceCollection().BuildServiceProvider(); - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -271,7 +271,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task WillReplaceServiceProviderFeatureWithNullRequestServices() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -293,7 +293,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanAccessLogger() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -311,7 +311,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanAccessHttpContext() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -343,7 +343,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanAddNewHostServices() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -366,7 +366,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CreateInvokesApp() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(context => { @@ -383,7 +383,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task DisposeStreamIgnored() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(async context => { @@ -402,7 +402,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task DisposedServerThrows() { - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { app.Run(async context => { @@ -422,7 +422,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CancelAborts() { - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .Configure(app => { app.Run(context => @@ -441,7 +441,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanCreateViaStartupType() { - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .UseStartup(); var server = new TestServer(builder); HttpResponseMessage result = await server.CreateClient().GetAsync("/"); @@ -453,7 +453,7 @@ namespace Microsoft.AspNet.TestHost [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Hangs randomly (issue #507)")] public async Task CanCreateViaStartupTypeAndSpecifyEnv() { - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .UseStartup() .UseEnvironment("Foo"); var server = new TestServer(builder); @@ -469,7 +469,7 @@ namespace Microsoft.AspNet.TestHost { DiagnosticListener diagnosticListener = null; - var builder = new WebApplicationBuilder() + var builder = new WebHostBuilder() .Configure(app => { diagnosticListener = app.ApplicationServices.GetRequiredService(); @@ -498,7 +498,7 @@ namespace Microsoft.AspNet.TestHost public async Task ExceptionDiagnosticAvailable() { DiagnosticListener diagnosticListener = null; - var builder = new WebApplicationBuilder().Configure(app => + var builder = new WebHostBuilder().Configure(app => { diagnosticListener = app.ApplicationServices.GetRequiredService(); app.Run(context =>