diff --git a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs index 6a425883ea..679e3078a4 100644 --- a/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/GenericWebHostBuilder.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// 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; @@ -196,16 +196,12 @@ namespace Microsoft.AspNetCore.Hosting.Internal public IWebHostBuilder UseDefaultServiceProvider(Action configure) { - // REVIEW: This is a hack to change the builder with the HostBuilderContext in scope, - // we're not actually using configuration here - _builder.ConfigureAppConfiguration((context, _) => + _builder.UseServiceProviderFactory(context => { var webHostBuilderContext = GetWebHostBuilderContext(context); var options = new ServiceProviderOptions(); configure(webHostBuilderContext, options); - - // This is only fine because this runs last - _builder.UseServiceProviderFactory(new DefaultServiceProviderFactory(options)); + return new DefaultServiceProviderFactory(options); }); return this; @@ -373,4 +369,4 @@ namespace Microsoft.AspNetCore.Hosting.Internal } } } -} \ No newline at end of file +} diff --git a/src/Hosting/samples/GenericWebHost/FakeServer.cs b/src/Hosting/samples/GenericWebHost/FakeServer.cs deleted file mode 100644 index bb1d669846..0000000000 --- a/src/Hosting/samples/GenericWebHost/FakeServer.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Http.Features; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; - -namespace GenericWebHost -{ - // We can't reference real servers in this sample without creating a circular repo dependency. - // This fake server lets us at least run the code. - public class FakeServer : IServer - { - public IFeatureCollection Features => new FeatureCollection(); - - public Task StartAsync(IHttpApplication application, CancellationToken cancellationToken) => Task.CompletedTask; - - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; - - public void Dispose() - { - } - } - - public static class FakeServerWebHostBuilderExtensions - { - public static IHostBuilder UseFakeServer(this IHostBuilder builder) - { - return builder.ConfigureServices((builderContext, services) => services.AddSingleton()); - } - } -} diff --git a/src/Hosting/samples/GenericWebHost/GenericWebHost.csproj b/src/Hosting/samples/GenericWebHost/GenericWebHost.csproj index 911fbc22eb..120b4b4276 100644 --- a/src/Hosting/samples/GenericWebHost/GenericWebHost.csproj +++ b/src/Hosting/samples/GenericWebHost/GenericWebHost.csproj @@ -9,10 +9,8 @@ + - - - diff --git a/src/Hosting/samples/GenericWebHost/Program.cs b/src/Hosting/samples/GenericWebHost/Program.cs index 653541ef7a..34eb0faa21 100644 --- a/src/Hosting/samples/GenericWebHost/Program.cs +++ b/src/Hosting/samples/GenericWebHost/Program.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; @@ -11,17 +11,11 @@ namespace GenericWebHost { public static async Task Main(string[] args) { - var host = new HostBuilder() - .ConfigureAppConfiguration((hostContext, config) => - { - config.AddEnvironmentVariables(); - config.AddJsonFile("appsettings.json", optional: true); - config.AddCommandLine(args); - }) - .UseFakeServer() + var host = Host.CreateDefaultBuilder() .ConfigureWebHost(builder => { - builder.Configure(app => + builder.UseKestrel() + .Configure(app => { app.Run(async (context) => { @@ -29,7 +23,6 @@ namespace GenericWebHost }); }); }) - .UseConsoleLifetime() .Build(); await host.RunAsync(); diff --git a/src/Hosting/samples/GenericWebHost/WebHostExtensions.cs b/src/Hosting/samples/GenericWebHost/WebHostExtensions.cs deleted file mode 100644 index bf5567d81a..0000000000 --- a/src/Hosting/samples/GenericWebHost/WebHostExtensions.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Text; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Hosting.Internal; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.ObjectPool; - -namespace GenericWebHost -{ - public static class WebHostExtensions - { - public static IHostBuilder ConfigureWebHost(this IHostBuilder builder, Action configureApp) - { - return builder.ConfigureServices((bulderContext, services) => - { - services.Configure(options => - { - options.ConfigureApp = configureApp; - }); - services.AddHostedService(); - - var listener = new DiagnosticListener("Microsoft.AspNetCore"); - services.AddSingleton(listener); - services.AddSingleton(listener); - - services.AddTransient(); - services.AddScoped(); - - // Conjure up a RequestServices - services.AddTransient(); - services.AddTransient, DefaultServiceProviderFactory>(); - - // Ensure object pooling is available everywhere. - services.AddSingleton(); - }); - } - } -} diff --git a/src/Hosting/samples/GenericWebHost/WebHostService.cs b/src/Hosting/samples/GenericWebHost/WebHostService.cs deleted file mode 100644 index 1ac316178f..0000000000 --- a/src/Hosting/samples/GenericWebHost/WebHostService.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Diagnostics; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Builder.Internal; -using Microsoft.AspNetCore.Hosting.Internal; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Hosting.Server.Features; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; - -namespace GenericWebHost -{ - internal class WebHostService : IHostedService - { - public WebHostService(IOptions options, IServiceProvider services, HostBuilderContext hostBuilderContext, IServer server, - ILogger logger, DiagnosticListener diagnosticListener, IHttpContextFactory httpContextFactory) - { - Options = options?.Value ?? throw new System.ArgumentNullException(nameof(options)); - - if (Options.ConfigureApp == null) - { - throw new ArgumentException(nameof(Options.ConfigureApp)); - } - - Services = services ?? throw new ArgumentNullException(nameof(services)); - HostBuilderContext = hostBuilderContext ?? throw new ArgumentNullException(nameof(hostBuilderContext)); - Server = server ?? throw new ArgumentNullException(nameof(server)); - Logger = logger ?? throw new ArgumentNullException(nameof(logger)); - DiagnosticListener = diagnosticListener ?? throw new ArgumentNullException(nameof(diagnosticListener)); - HttpContextFactory = httpContextFactory ?? throw new ArgumentNullException(nameof(httpContextFactory)); - } - - public WebHostServiceOptions Options { get; } - public IServiceProvider Services { get; } - public HostBuilderContext HostBuilderContext { get; } - public IServer Server { get; } - public ILogger Logger { get; } - public DiagnosticListener DiagnosticListener { get; } - public IHttpContextFactory HttpContextFactory { get; } - - public Task StartAsync(CancellationToken cancellationToken) - { - Server.Features.Get()?.Addresses.Add("http://localhost:5000"); - - var builder = new ApplicationBuilder(Services, Server.Features); - Options.ConfigureApp(HostBuilderContext, builder); - var app = builder.Build(); - - var httpApp = new HostingApplication(app, Logger, DiagnosticListener, HttpContextFactory); - return Server.StartAsync(httpApp, cancellationToken); - } - - public Task StopAsync(CancellationToken cancellationToken) - { - return Server.StopAsync(cancellationToken); - } - } -} \ No newline at end of file diff --git a/src/Hosting/samples/GenericWebHost/WebHostServiceOptions.cs b/src/Hosting/samples/GenericWebHost/WebHostServiceOptions.cs deleted file mode 100644 index 123dcf8790..0000000000 --- a/src/Hosting/samples/GenericWebHost/WebHostServiceOptions.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.Hosting; - -namespace GenericWebHost -{ - public class WebHostServiceOptions - { - public Action ConfigureApp { get; internal set; } - } -} \ No newline at end of file diff --git a/src/Hosting/samples/SampleStartups/FakeServer.cs b/src/Hosting/samples/SampleStartups/FakeServer.cs deleted file mode 100644 index ebdfdb383e..0000000000 --- a/src/Hosting/samples/SampleStartups/FakeServer.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Hosting.Server; -using Microsoft.AspNetCore.Http.Features; -using Microsoft.Extensions.DependencyInjection; - -namespace SampleStartups -{ - // We can't reference real servers in this sample without creating a circular repo dependency. - // This fake server lets us at least run the code. - public class FakeServer : IServer - { - public IFeatureCollection Features => new FeatureCollection(); - - public Task StartAsync(IHttpApplication application, CancellationToken cancellationToken) => Task.CompletedTask; - - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; - - public void Dispose() - { - } - } - - public static class FakeServerWebHostBuilderExtensions - { - public static IWebHostBuilder UseFakeServer(this IWebHostBuilder builder) - { - return builder.ConfigureServices(services => services.AddSingleton()); - } - } -} diff --git a/src/Hosting/samples/SampleStartups/SampleStartups.csproj b/src/Hosting/samples/SampleStartups/SampleStartups.csproj index f2a15d2697..1efd69367b 100644 --- a/src/Hosting/samples/SampleStartups/SampleStartups.csproj +++ b/src/Hosting/samples/SampleStartups/SampleStartups.csproj @@ -1,13 +1,14 @@ - + netcoreapp3.0 - SampleStartups.StartupInjection + SampleStartups.StartupBlockingOnStart exe + diff --git a/src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs b/src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs index 65a226f3cd..dca16710e9 100644 --- a/src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs +++ b/src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs @@ -33,7 +33,7 @@ namespace SampleStartups var host = new WebHostBuilder() .UseConfiguration(config) - .UseFakeServer() + .UseKestrel() .UseStartup() .Build(); diff --git a/src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs b/src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs index 8413c47c90..294497bbf7 100644 --- a/src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs +++ b/src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs @@ -25,7 +25,7 @@ namespace SampleStartups var host = new WebHostBuilder() .UseConfiguration(config) - .UseFakeServer() + .UseKestrel() .UseStartup() .UseUrls("http://localhost:5000", "http://localhost:5001") .Build(); diff --git a/src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs b/src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs index ba45235a6a..296fcc0b0f 100644 --- a/src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs +++ b/src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs @@ -30,8 +30,7 @@ namespace SampleStartups public void Start() { _host = new WebHostBuilder() - //.UseKestrel() - .UseFakeServer() + .UseKestrel() .UseStartup() .Start(_urls.ToArray()); } diff --git a/src/Hosting/samples/SampleStartups/StartupFullControl.cs b/src/Hosting/samples/SampleStartups/StartupFullControl.cs index a7986d9e0c..f438111050 100644 --- a/src/Hosting/samples/SampleStartups/StartupFullControl.cs +++ b/src/Hosting/samples/SampleStartups/StartupFullControl.cs @@ -22,8 +22,7 @@ namespace SampleStartups var host = new WebHostBuilder() .UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden - //.UseKestrel() - .UseFakeServer() + .UseKestrel() .UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory .UseUrls("http://*:1000", "https://*:902") .UseEnvironment(EnvironmentName.Development) diff --git a/src/Hosting/samples/SampleStartups/StartupHelloWorld.cs b/src/Hosting/samples/SampleStartups/StartupHelloWorld.cs index 88a77cfc6c..a02513d201 100644 --- a/src/Hosting/samples/SampleStartups/StartupHelloWorld.cs +++ b/src/Hosting/samples/SampleStartups/StartupHelloWorld.cs @@ -21,8 +21,7 @@ namespace SampleStartups public static void Main(string[] args) { var host = new WebHostBuilder() - //.UseKestrel() - .UseFakeServer() + .UseKestrel() .UseStartup() .Build(); diff --git a/src/Hosting/samples/SampleStartups/StartupInjection.cs b/src/Hosting/samples/SampleStartups/StartupInjection.cs index 381d621a10..163ac165a6 100644 --- a/src/Hosting/samples/SampleStartups/StartupInjection.cs +++ b/src/Hosting/samples/SampleStartups/StartupInjection.cs @@ -1,4 +1,4 @@ -using System; +using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; @@ -20,8 +20,7 @@ namespace SampleStartups public static void Main(string[] args) { var host = new WebHostBuilder() - //.UseKestrel() - .UseFakeServer() + .UseKestrel() // Each of these three sets ApplicationName to the current assembly, which is needed in order to // scan the assembly for HostingStartupAttributes. // .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")