[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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config) .UseConfiguration(config)
.UseKestrel() .UseKestrel()
.UseStartup<StartupBlockingOnStart>() .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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config) .UseConfiguration(config)
.UseKestrel() .UseKestrel()
.UseStartup<StartupConfigureAddresses>() .UseStartup<StartupConfigureAddresses>()
.UseUrls("http://localhost:5000", "http://localhost:5001") .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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel() .UseKestrel()
.UseStartup<StartupExternallyControlled>() .UseStartup<StartupExternallyControlled>()
.Start(_urls.ToArray()); .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,18 +22,16 @@ namespace SampleStartups
.AddCommandLine(args) .AddCommandLine(args)
.Build(); .Build();
var host = new WebHostBuilder() var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden .UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
.UseKestrel() .UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory .UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
.UseUrls("http://*:1000", "https://*:902") .UseUrls("http://*:1000", "https://*:902")
.UseEnvironment(Environments.Development) .UseEnvironment(Environments.Development)
.UseWebRoot("public") .UseWebRoot("public")
.ConfigureServices(services =>
{
// Configure services that the application can see
services.AddSingleton<IMyCustomService, MyCustomService>();
})
.Configure(app => .Configure(app =>
{ {
// Write the application inline, this won't call any startup class in the assembly // Write the application inline, this won't call any startup class in the assembly
@ -41,10 +40,16 @@ namespace SampleStartups
{ {
return next(context); return next(context);
}); });
});
})
.ConfigureServices(services =>
{
// Configure services that the application can see
services.AddSingleton<IMyCustomService, MyCustomService>();
}) })
.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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel() .UseKestrel()
.UseStartup<StartupHelloWorld>() .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()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel() .UseKestrel()
// Each of these three sets ApplicationName to the current assembly, which is needed in order to // Each of these three sets ApplicationName to the current assembly, which is needed in order to
// scan the assembly for HostingStartupAttributes. // scan the assembly for HostingStartupAttributes.
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups") // .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
// .Configure(_ => { }) // .Configure(_ => { })
.UseStartup<NormalStartup>() .UseStartup<NormalStartup>();
})
.Build(); .Build();
host.Run(); return host.RunAsync();
} }
} }