Skip to content

Commit 747957b

Browse files
authored
[Hosting] Move to GenericHost (#24297)
1 parent 97ced4e commit 747957b

File tree

6 files changed

+86
-50
lines changed

6 files changed

+86
-50
lines changed

src/Hosting/samples/SampleStartups/StartupBlockingOnStart.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.Extensions.Configuration;
67
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
79

810
// Note that this sample will not run. It is only here to illustrate usage patterns.
911

@@ -27,20 +29,25 @@ public override void Configure(IApplicationBuilder app)
2729
}
2830

2931
// Entry point for the application.
30-
public static void Main(string[] args)
32+
public static async Task Main(string[] args)
3133
{
3234
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
3335

34-
var host = new WebHostBuilder()
35-
.UseConfiguration(config)
36-
.UseKestrel()
37-
.UseStartup<StartupBlockingOnStart>()
36+
var host = new HostBuilder()
37+
.ConfigureWebHost(webHostBuilder =>
38+
{
39+
webHostBuilder
40+
.UseConfiguration(config)
41+
.UseKestrel()
42+
.UseStartup<StartupBlockingOnStart>();
43+
})
3844
.Build();
3945

4046
using (host)
4147
{
42-
host.Start();
48+
await host.StartAsync();
4349
Console.ReadLine();
50+
await host.StopAsync();
4451
}
4552
}
4653
}

src/Hosting/samples/SampleStartups/StartupConfigureAddresses.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
using System.Threading.Tasks;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Http;
45
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.Hosting;
57

68
// Note that this sample will not run. It is only here to illustrate usage patterns.
79

@@ -19,18 +21,22 @@ public override void Configure(IApplicationBuilder app)
1921
}
2022

2123
// Entry point for the application.
22-
public static void Main(string[] args)
24+
public static Task Main(string[] args)
2325
{
2426
var config = new ConfigurationBuilder().AddCommandLine(args).Build();
2527

26-
var host = new WebHostBuilder()
27-
.UseConfiguration(config)
28-
.UseKestrel()
29-
.UseStartup<StartupConfigureAddresses>()
30-
.UseUrls("http://localhost:5000", "http://localhost:5001")
28+
var host = new HostBuilder()
29+
.ConfigureWebHost(webHostBuilder =>
30+
{
31+
webHostBuilder
32+
.UseConfiguration(config)
33+
.UseKestrel()
34+
.UseStartup<StartupConfigureAddresses>()
35+
.UseUrls("http://localhost:5000", "http://localhost:5001");
36+
})
3137
.Build();
3238

33-
host.Run();
39+
return host.RunAsync();
3440
}
3541
}
3642
}

src/Hosting/samples/SampleStartups/StartupExternallyControlled.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Hosting;
66
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.Hosting;
78

89
// Note that this sample will not run. It is only here to illustrate usage patterns.
910

1011
namespace SampleStartups
1112
{
1213
public class StartupExternallyControlled : StartupBase
1314
{
14-
private IWebHost _host;
15+
private IHost _host;
1516
private readonly List<string> _urls = new List<string>();
1617

1718
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -29,10 +30,15 @@ public StartupExternallyControlled()
2930

3031
public void Start()
3132
{
32-
_host = new WebHostBuilder()
33-
.UseKestrel()
34-
.UseStartup<StartupExternallyControlled>()
35-
.Start(_urls.ToArray());
33+
_host = new HostBuilder()
34+
.ConfigureWebHost(webHostBuilder =>
35+
{
36+
webHostBuilder
37+
.UseKestrel()
38+
.UseStartup<StartupExternallyControlled>()
39+
.UseUrls(_urls.ToArray());
40+
})
41+
.Start();
3642
}
3743

3844
public async Task StopAsync()

src/Hosting/samples/SampleStartups/StartupFullControl.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Threading.Tasks;
34
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Hosting;
56
using Microsoft.Extensions.Configuration;
@@ -13,38 +14,42 @@ namespace SampleStartups
1314
{
1415
public class StartupFullControl
1516
{
16-
public static void Main(string[] args)
17+
public static Task Main(string[] args)
1718
{
1819
var config = new ConfigurationBuilder()
1920
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
2021
.AddJsonFile("hosting.json", optional: true)
2122
.AddCommandLine(args)
2223
.Build();
2324

24-
var host = new WebHostBuilder()
25-
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
26-
.UseKestrel()
27-
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
28-
.UseUrls("http://*:1000", "https://*:902")
29-
.UseEnvironment(Environments.Development)
30-
.UseWebRoot("public")
25+
var host = new HostBuilder()
26+
.ConfigureWebHost(webHostBuilder =>
27+
{
28+
webHostBuilder
29+
.UseConfiguration(config) // Default set of configurations to use, may be subsequently overridden
30+
.UseKestrel()
31+
.UseContentRoot(Directory.GetCurrentDirectory()) // Override the content root with the current directory
32+
.UseUrls("http://*:1000", "https://*:902")
33+
.UseEnvironment(Environments.Development)
34+
.UseWebRoot("public")
35+
.Configure(app =>
36+
{
37+
// Write the application inline, this won't call any startup class in the assembly
38+
39+
app.Use(next => context =>
40+
{
41+
return next(context);
42+
});
43+
});
44+
})
3145
.ConfigureServices(services =>
3246
{
3347
// Configure services that the application can see
3448
services.AddSingleton<IMyCustomService, MyCustomService>();
3549
})
36-
.Configure(app =>
37-
{
38-
// Write the application inline, this won't call any startup class in the assembly
39-
40-
app.Use(next => context =>
41-
{
42-
return next(context);
43-
});
44-
})
4550
.Build();
4651

47-
host.Run();
52+
return host.RunAsync();
4853
}
4954
}
5055

src/Hosting/samples/SampleStartups/StartupHelloWorld.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
using System.Threading.Tasks;
12
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Http;
5+
using Microsoft.Extensions.Hosting;
46

57
// Note that this sample will not run. It is only here to illustrate usage patterns.
68

@@ -18,14 +20,18 @@ public override void Configure(IApplicationBuilder app)
1820
}
1921

2022
// Entry point for the application.
21-
public static void Main(string[] args)
23+
public static Task Main(string[] args)
2224
{
23-
var host = new WebHostBuilder()
24-
.UseKestrel()
25-
.UseStartup<StartupHelloWorld>()
25+
var host = new HostBuilder()
26+
.ConfigureWebHost(webHostBuilder =>
27+
{
28+
webHostBuilder
29+
.UseKestrel()
30+
.UseStartup<StartupHelloWorld>();
31+
})
2632
.Build();
2733

28-
host.Run();
34+
return host.RunAsync();
2935
}
3036
}
3137
}

src/Hosting/samples/SampleStartups/StartupInjection.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Threading.Tasks;
23
using Microsoft.AspNetCore.Builder;
34
using Microsoft.AspNetCore.Hosting;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
68

79
// HostingStartup's in the primary assembly are run automatically.
810
[assembly: HostingStartup(typeof(SampleStartups.StartupInjection))]
@@ -17,18 +19,22 @@ public void Configure(IWebHostBuilder builder)
1719
}
1820

1921
// Entry point for the application.
20-
public static void Main(string[] args)
22+
public static Task Main(string[] args)
2123
{
22-
var host = new WebHostBuilder()
23-
.UseKestrel()
24-
// Each of these three sets ApplicationName to the current assembly, which is needed in order to
25-
// scan the assembly for HostingStartupAttributes.
26-
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
27-
// .Configure(_ => { })
28-
.UseStartup<NormalStartup>()
24+
var host = new HostBuilder()
25+
.ConfigureWebHost(webHostBuilder =>
26+
{
27+
webHostBuilder
28+
.UseKestrel()
29+
// Each of these three sets ApplicationName to the current assembly, which is needed in order to
30+
// scan the assembly for HostingStartupAttributes.
31+
// .UseSetting(WebHostDefaults.ApplicationKey, "SampleStartups")
32+
// .Configure(_ => { })
33+
.UseStartup<NormalStartup>();
34+
})
2935
.Build();
3036

31-
host.Run();
37+
return host.RunAsync();
3238
}
3339
}
3440

0 commit comments

Comments
 (0)