Skip to content

Commit 23e48c6

Browse files
Transform first 3 files from WebHostBuilder to HostBuilder pattern
Co-authored-by: BrennanConroy <[email protected]>
1 parent 02299e4 commit 23e48c6

File tree

4 files changed

+60
-35
lines changed

4 files changed

+60
-35
lines changed

src/Mvc/perf/benchmarkapps/BasicApi/Startup.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Authentication.JwtBearer;
1212
using Microsoft.AspNetCore.Builder;
1313
using Microsoft.AspNetCore.Hosting;
14+
using Microsoft.Extensions.Hosting;
1415
using Microsoft.EntityFrameworkCore;
1516
using Microsoft.EntityFrameworkCore.Infrastructure;
1617
using Microsoft.EntityFrameworkCore.Migrations;
@@ -229,25 +230,29 @@ private void DropDatabaseTables(IServiceProvider services)
229230

230231
public static void Main(string[] args)
231232
{
232-
var host = CreateWebHostBuilder(args)
233+
using var host = CreateHost(args)
233234
.Build();
234235

235236
host.Run();
236237
}
237238

238-
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
239+
public static IHostBuilder CreateHost(string[] args)
239240
{
240241
var configuration = new ConfigurationBuilder()
241242
.AddEnvironmentVariables()
242243
.AddCommandLine(args)
243244
.Build();
244245

245-
return new WebHostBuilder()
246-
.UseKestrel()
247-
.UseUrls("http://+:5000")
248-
.UseConfiguration(configuration)
249-
.UseContentRoot(Directory.GetCurrentDirectory())
250-
.UseStartup<Startup>();
246+
return new HostBuilder()
247+
.ConfigureWebHost(webHostBuilder =>
248+
{
249+
webHostBuilder
250+
.UseKestrel()
251+
.UseUrls("http://+:5000")
252+
.UseConfiguration(configuration)
253+
.UseContentRoot(Directory.GetCurrentDirectory())
254+
.UseStartup<Startup>();
255+
});
251256
}
252257
}
253258
}

src/Mvc/samples/MvcSandbox/Startup.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Hosting;
5+
46
namespace MvcSandbox;
57

68
public class Startup
@@ -42,22 +44,26 @@ static void ConfigureEndpoints(IEndpointRouteBuilder endpoints)
4244

4345
public static void Main(string[] args)
4446
{
45-
var host = CreateWebHostBuilder(args)
47+
using var host = CreateHost(args)
4648
.Build();
4749

4850
host.Run();
4951
}
5052

51-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
52-
new WebHostBuilder()
53-
.UseContentRoot(Directory.GetCurrentDirectory())
54-
.ConfigureLogging(factory =>
53+
public static IHostBuilder CreateHost(string[] args) =>
54+
new HostBuilder()
55+
.ConfigureWebHost(webHostBuilder =>
5556
{
56-
factory
57-
.AddConsole()
58-
.AddDebug();
59-
})
60-
.UseKestrel()
61-
.UseStartup<Startup>();
57+
webHostBuilder
58+
.UseContentRoot(Directory.GetCurrentDirectory())
59+
.ConfigureLogging(factory =>
60+
{
61+
factory
62+
.AddConsole()
63+
.AddDebug();
64+
})
65+
.UseKestrel()
66+
.UseStartup<Startup>();
67+
});
6268
}
6369

src/Mvc/test/WebSites/BasicWebSite/Program.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Hosting;
5+
46
namespace BasicWebSite;
57

68
public class Program
79
{
8-
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
10+
public static void Main(string[] args)
11+
{
12+
using var host = CreateHost(args).Build();
13+
host.Run();
14+
}
915

10-
// Do not change. This is the pattern our test infrastructure uses to initialize a IWebHostBuilder from
11-
// a users app.
12-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
13-
new WebHostBuilder()
14-
.UseContentRoot(Directory.GetCurrentDirectory())
15-
.UseStartup<StartupWithoutEndpointRouting>()
16-
.UseKestrel()
17-
.UseIISIntegration();
16+
// This method now returns IHostBuilder and uses the new pattern with HostBuilder and ConfigureWebHost
17+
public static IHostBuilder CreateHost(string[] args) =>
18+
new HostBuilder()
19+
.ConfigureWebHost(webHostBuilder =>
20+
{
21+
webHostBuilder
22+
.UseContentRoot(Directory.GetCurrentDirectory())
23+
.UseStartup<StartupWithoutEndpointRouting>()
24+
.UseKestrel()
25+
.UseIISIntegration();
26+
});
1827
}
1928

2029
public class TestService

src/Mvc/test/WebSites/SimpleWebSite/Startup.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.Extensions.Hosting;
45
using Microsoft.Net.Http.Headers;
56
using Newtonsoft.Json;
67

@@ -29,17 +30,21 @@ public void Configure(IApplicationBuilder app)
2930

3031
public static void Main(string[] args)
3132
{
32-
var host = CreateWebHostBuilder(args)
33+
using var host = CreateHost(args)
3334
.Build();
3435

3536
host.Run();
3637
}
3738

38-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
39-
new WebHostBuilder()
40-
.UseContentRoot(Directory.GetCurrentDirectory())
41-
.UseStartup<Startup>()
42-
.UseKestrel()
43-
.UseIISIntegration();
39+
public static IHostBuilder CreateHost(string[] args) =>
40+
new HostBuilder()
41+
.ConfigureWebHost(webHostBuilder =>
42+
{
43+
webHostBuilder
44+
.UseContentRoot(Directory.GetCurrentDirectory())
45+
.UseStartup<Startup>()
46+
.UseKestrel()
47+
.UseIISIntegration();
48+
});
4449
}
4550

0 commit comments

Comments
 (0)