[Hosting] Move to GenericHost (#24297)

This commit is contained in:
Kahbazi 2020-07-28 02:59:00 +04:30 committed by GitHub
parent 97ced4e7c9
commit 747957bb40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 86 additions and 50 deletions

View File

@ -1,9 +1,11 @@
using System; using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
// Note that this sample will not run. It is only here to illustrate usage patterns. // Note that this sample will not run. It is only here to illustrate usage patterns.
@ -27,20 +29,25 @@ namespace SampleStartups
} }
// Entry point for the application. // Entry point for the application.
public static void Main(string[] args) public static async Task Main(string[] args)
{ {
var config = new ConfigurationBuilder().AddCommandLine(args).Build(); var config = new ConfigurationBuilder().AddCommandLine(args).Build();
var host = new WebHostBuilder() var host = new HostBuilder()
.UseConfiguration(config) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseStartup<StartupBlockingOnStart>() webHostBuilder
.UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupBlockingOnStart>();
})
.Build(); .Build();
using (host) using (host)
{ {
host.Start(); await host.StartAsync();
Console.ReadLine(); Console.ReadLine();
await host.StopAsync();
} }
} }
} }

View File

@ -1,7 +1,9 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
// Note that this sample will not run. It is only here to illustrate usage patterns. // Note that this sample will not run. It is only here to illustrate usage patterns.
@ -19,18 +21,22 @@ namespace SampleStartups
} }
// Entry point for the application. // Entry point for the application.
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var config = new ConfigurationBuilder().AddCommandLine(args).Build(); var config = new ConfigurationBuilder().AddCommandLine(args).Build();
var host = new WebHostBuilder() var host = new HostBuilder()
.UseConfiguration(config) .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseStartup<StartupConfigureAddresses>() webHostBuilder
.UseUrls("http://localhost:5000", "http://localhost:5001") .UseConfiguration(config)
.UseKestrel()
.UseStartup<StartupConfigureAddresses>()
.UseUrls("http://localhost:5000", "http://localhost:5001");
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -4,6 +4,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
// Note that this sample will not run. It is only here to illustrate usage patterns. // Note that this sample will not run. It is only here to illustrate usage patterns.
@ -11,7 +12,7 @@ namespace SampleStartups
{ {
public class StartupExternallyControlled : StartupBase public class StartupExternallyControlled : StartupBase
{ {
private IWebHost _host; private IHost _host;
private readonly List<string> _urls = new List<string>(); private readonly List<string> _urls = new List<string>();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@ -29,10 +30,15 @@ namespace SampleStartups
public void Start() public void Start()
{ {
_host = new WebHostBuilder() _host = new HostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseStartup<StartupExternallyControlled>() {
.Start(_urls.ToArray()); webHostBuilder
.UseKestrel()
.UseStartup<StartupExternallyControlled>()
.UseUrls(_urls.ToArray());
})
.Start();
} }
public async Task StopAsync() public async Task StopAsync()

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@ -13,7 +14,7 @@ namespace SampleStartups
{ {
public class StartupFullControl public class StartupFullControl
{ {
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var config = new ConfigurationBuilder() var config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_") .AddEnvironmentVariables(prefix: "ASPNETCORE_")
@ -21,30 +22,34 @@ namespace SampleStartups
.AddCommandLine(args) .AddCommandLine(args)
.Build(); .Build();
var host = new WebHostBuilder() var host = new HostBuilder()
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden .ConfigureWebHost(webHostBuilder =>
.UseKestrel() {
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory webHostBuilder
.UseUrls("http://*:1000", "https://*:902") .UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
.UseEnvironment(Environments.Development) .UseKestrel()
.UseWebRoot("public") .UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902")
.UseEnvironment(Environments.Development)
.UseWebRoot("public")
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly
app.Use(next => context =>
{
return next(context);
});
});
})
.ConfigureServices(services => .ConfigureServices(services =>
{ {
// Configure services that the application can see // Configure services that the application can see
services.AddSingleton<IMyCustomService, MyCustomService>(); services.AddSingleton<IMyCustomService, MyCustomService>();
}) })
.Configure(app =>
{
// Write the application inline, this won't call any startup class in the assembly
app.Use(next => context =>
{
return next(context);
});
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }

View File

@ -1,6 +1,8 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
// Note that this sample will not run. It is only here to illustrate usage patterns. // Note that this sample will not run. It is only here to illustrate usage patterns.
@ -18,14 +20,18 @@ namespace SampleStartups
} }
// Entry point for the application. // Entry point for the application.
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
.UseStartup<StartupHelloWorld>() {
webHostBuilder
.UseKestrel()
.UseStartup<StartupHelloWorld>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }
} }

View File

@ -1,8 +1,10 @@
using System; using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
// HostingStartup's in the primary assembly are run automatically. // HostingStartup's in the primary assembly are run automatically.
[assembly: HostingStartup(typeof(SampleStartups.StartupInjection))] [assembly: HostingStartup(typeof(SampleStartups.StartupInjection))]
@ -17,18 +19,22 @@ namespace SampleStartups
} }
// Entry point for the application. // Entry point for the application.
public static void Main(string[] args) public static Task Main(string[] args)
{ {
var host = new WebHostBuilder() var host = new HostBuilder()
.UseKestrel() .ConfigureWebHost(webHostBuilder =>
// Each of these three sets ApplicationName to the current assembly, which is needed in order to {
// scan the assembly for HostingStartupAttributes. webHostBuilder
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups") .UseKestrel()
// .Configure(_ => { }) // Each of these three sets ApplicationName to the current assembly, which is needed in order to
.UseStartup<NormalStartup>() // scan the assembly for HostingStartupAttributes.
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
// .Configure(_ => { })
.UseStartup<NormalStartup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }