Skip to content

Commit ac4fcb6

Browse files
Convert sample apps from WebHost to HostBuilder pattern
Co-authored-by: BrennanConroy <[email protected]>
1 parent 4c94381 commit ac4fcb6

File tree

13 files changed

+149
-107
lines changed

13 files changed

+149
-107
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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.AspNetCore;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
56

67
namespace ComponentsApp.Server;
78

@@ -12,10 +13,13 @@ public static void Main(string[] args)
1213
BuildWebHost(args).Run();
1314
}
1415

15-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
16-
WebHost.CreateDefaultBuilder(args)
17-
.UseStartup<Startup>();
16+
public static IHostBuilder CreateHostBuilder(string[] args) =>
17+
Host.CreateDefaultBuilder(args)
18+
.ConfigureWebHostDefaults(webBuilder =>
19+
{
20+
webBuilder.UseStartup<Startup>();
21+
});
1822

19-
public static IWebHost BuildWebHost(string[] args) =>
20-
CreateWebHostBuilder(args).Build();
23+
public static IHost BuildWebHost(string[] args) =>
24+
CreateHostBuilder(args).Build();
2125
}

src/DefaultBuilder/samples/SampleApp/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ private static void CustomApplicationBuilder()
7575
}
7676
private static void DirectWebHost(string[] args)
7777
{
78-
// Using defaults with a Startup class
79-
using (var host = WebHost.CreateDefaultBuilder(args)
80-
.UseStartup<Startup>()
78+
// Using defaults with HostBuilder pattern
79+
using (var host = Host.CreateDefaultBuilder(args)
80+
.ConfigureWebHostDefaults(webBuilder =>
81+
{
82+
webBuilder.UseStartup<Startup>();
83+
})
8184
.Build())
8285
{
8386
host.Run();

src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#pragma warning disable CS0618 // Type or member is obsolete
55

66
using System;
7-
using Microsoft.AspNetCore;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Hosting;
109
using Microsoft.AspNetCore.Http;
1110
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
1212

1313
namespace CreateDefaultBuilderApp;
1414

@@ -18,23 +18,27 @@ static void Main(string[] args)
1818
{
1919
string responseMessage = null;
2020

21-
WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
22-
.ConfigureServices((context, services) => responseMessage = responseMessage ?? GetResponseMessage(context))
23-
.ConfigureKestrel(options => options
24-
.Configure(options.ConfigurationLoader.Configuration)
25-
.Endpoint("HTTP", endpointOptions =>
26-
{
27-
if (responseMessage == null
28-
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
29-
{
30-
responseMessage = "Default Kestrel configuration not read.";
31-
}
32-
}))
33-
.Configure(app => app.Run(context =>
21+
Host.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
22+
.ConfigureWebHostDefaults(webBuilder =>
3423
{
35-
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
36-
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
37-
}))
24+
webBuilder
25+
.ConfigureServices((context, services) => responseMessage = responseMessage ?? GetResponseMessage(context))
26+
.ConfigureKestrel(options => options
27+
.Configure(options.ConfigurationLoader.Configuration)
28+
.Endpoint("HTTP", endpointOptions =>
29+
{
30+
if (responseMessage == null
31+
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
32+
{
33+
responseMessage = "Default Kestrel configuration not read.";
34+
}
35+
}))
36+
.Configure(app => app.Run(context =>
37+
{
38+
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
39+
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
40+
}));
41+
})
3842
.Build().Run();
3943
}
4044

src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#pragma warning disable CS0618 // Type or member is obsolete
55

66
using System;
7-
using Microsoft.AspNetCore;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.HostFiltering;
109
using Microsoft.AspNetCore.Hosting;
@@ -21,31 +20,35 @@ static void Main(string[] args)
2120
{
2221
string responseMessage = null;
2322

24-
WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
25-
.ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context))
26-
.ConfigureKestrel(options => options
27-
.Configure(options.ConfigurationLoader.Configuration)
28-
.Endpoint("HTTP", endpointOptions =>
29-
{
30-
if (responseMessage == null
31-
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
32-
{
33-
responseMessage = "Default Kestrel configuration not read.";
34-
}
35-
}))
36-
.Configure(app => app.Run(context =>
23+
Host.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" })
24+
.ConfigureWebHostDefaults(webBuilder =>
3725
{
38-
// Verify allowed hosts were loaded
39-
var hostFilteringOptions = app.ApplicationServices.GetRequiredService<IOptions<HostFilteringOptions>>();
40-
var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts);
41-
if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal))
42-
{
43-
responseMessage = "AllowedHosts not loaded into Options.";
44-
}
26+
webBuilder
27+
.ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context))
28+
.ConfigureKestrel(options => options
29+
.Configure(options.ConfigurationLoader.Configuration)
30+
.Endpoint("HTTP", endpointOptions =>
31+
{
32+
if (responseMessage == null
33+
&& !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"]))
34+
{
35+
responseMessage = "Default Kestrel configuration not read.";
36+
}
37+
}))
38+
.Configure(app => app.Run(context =>
39+
{
40+
// Verify allowed hosts were loaded
41+
var hostFilteringOptions = app.ApplicationServices.GetRequiredService<IOptions<HostFilteringOptions>>();
42+
var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts);
43+
if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal))
44+
{
45+
responseMessage = "AllowedHosts not loaded into Options.";
46+
}
4547

46-
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
47-
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
48-
}))
48+
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
49+
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
50+
}));
51+
})
4952
.Build()
5053
.Run();
5154
}

src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,43 @@
44
#pragma warning disable CS0618 // Type or member is obsolete
55

66
using System;
7-
using Microsoft.AspNetCore;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Hosting;
109
using Microsoft.AspNetCore.Http;
1110
using Microsoft.Extensions.DependencyInjection;
11+
using Microsoft.Extensions.Hosting;
1212

1313
namespace CreateDefaultBuilderApp;
1414

1515
public class Program
1616
{
1717
static void Main(string[] args)
1818
{
19-
WebHost.CreateDefaultBuilder()
20-
.UseUrls("http://127.0.0.1:0")
21-
.ConfigureServices((context, services) =>
19+
Host.CreateDefaultBuilder()
20+
.ConfigureWebHostDefaults(webBuilder =>
2221
{
23-
services.AddSingleton(typeof(IService<>), typeof(Service<>));
24-
services.AddScoped<IAnotherService, AnotherService>();
25-
})
26-
.Configure(app =>
27-
{
28-
app.Run(context =>
29-
{
30-
try
22+
webBuilder
23+
.UseUrls("http://127.0.0.1:0")
24+
.ConfigureServices((context, services) =>
3125
{
32-
context.RequestServices.GetService<IService<IAnotherService>>();
33-
return context.Response.WriteAsync("Success");
34-
}
35-
catch (Exception ex)
26+
services.AddSingleton(typeof(IService<>), typeof(Service<>));
27+
services.AddScoped<IAnotherService, AnotherService>();
28+
})
29+
.Configure(app =>
3630
{
37-
return context.Response.WriteAsync(ex.ToString());
38-
}
39-
});
31+
app.Run(context =>
32+
{
33+
try
34+
{
35+
context.RequestServices.GetService<IService<IAnotherService>>();
36+
return context.Response.WriteAsync("Success");
37+
}
38+
catch (Exception ex)
39+
{
40+
return context.Response.WriteAsync(ex.ToString());
41+
}
42+
});
43+
});
4044
})
4145
.Build().Run();
4246
}

src/Security/Authentication/samples/SocialSample/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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.AspNetCore;
4+
using Microsoft.Extensions.Hosting;
55

66
namespace SocialSample;
77

88
public static class Program
99
{
1010
public static void Main(string[] args)
1111
{
12-
var host = WebHost.CreateDefaultBuilder(args)
13-
.UseStartup<Startup>()
12+
var host = Host.CreateDefaultBuilder(args)
13+
.ConfigureWebHostDefaults(webBuilder =>
14+
{
15+
webBuilder.UseStartup<Startup>();
16+
})
1417
.Build();
1518

1619
host.Run();

src/Security/samples/ClaimsTransformation/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.IO;
77
using System.Linq;
88
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore;
109
using Microsoft.AspNetCore.Hosting;
1110
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.Hosting;
1212
using Microsoft.Extensions.Logging;
1313

1414
namespace AuthSamples.ClaimsTransformer;
@@ -17,12 +17,15 @@ public class Program
1717
{
1818
public static void Main(string[] args)
1919
{
20-
CreateWebHostBuilder(args)
20+
CreateHostBuilder(args)
2121
.Build()
2222
.Run();
2323
}
2424

25-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
26-
WebHost.CreateDefaultBuilder(args)
27-
.UseStartup<Startup>();
25+
public static IHostBuilder CreateHostBuilder(string[] args) =>
26+
Host.CreateDefaultBuilder(args)
27+
.ConfigureWebHostDefaults(webBuilder =>
28+
{
29+
webBuilder.UseStartup<Startup>();
30+
});
2831
}

src/Security/samples/Cookies/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.IO;
77
using System.Linq;
88
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore;
109
using Microsoft.AspNetCore.Hosting;
1110
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.Hosting;
1212
using Microsoft.Extensions.Logging;
1313

1414
namespace AuthSamples.Cookies;
@@ -17,12 +17,15 @@ public class Program
1717
{
1818
public static void Main(string[] args)
1919
{
20-
CreateWebHostBuilder(args)
20+
CreateHostBuilder(args)
2121
.Build()
2222
.Run();
2323
}
2424

25-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
26-
WebHost.CreateDefaultBuilder(args)
27-
.UseStartup<Startup>();
25+
public static IHostBuilder CreateHostBuilder(string[] args) =>
26+
Host.CreateDefaultBuilder(args)
27+
.ConfigureWebHostDefaults(webBuilder =>
28+
{
29+
webBuilder.UseStartup<Startup>();
30+
});
2831
}
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
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.AspNetCore;
54
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.Extensions.Hosting;
66

77
namespace CustomPolicyProvider;
88

99
public class Program
1010
{
1111
public static void Main(string[] args)
1212
{
13-
CreateWebHostBuilder(args).Build().Run();
13+
CreateHostBuilder(args).Build().Run();
1414
}
1515

16-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
17-
WebHost.CreateDefaultBuilder(args)
18-
.UseStartup<Startup>();
16+
public static IHostBuilder CreateHostBuilder(string[] args) =>
17+
Host.CreateDefaultBuilder(args)
18+
.ConfigureWebHostDefaults(webBuilder =>
19+
{
20+
webBuilder.UseStartup<Startup>();
21+
});
1922
}

src/Security/samples/DynamicSchemes/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using System.IO;
77
using System.Linq;
88
using System.Threading.Tasks;
9-
using Microsoft.AspNetCore;
109
using Microsoft.AspNetCore.Hosting;
1110
using Microsoft.Extensions.Configuration;
11+
using Microsoft.Extensions.Hosting;
1212
using Microsoft.Extensions.Logging;
1313

1414
namespace AuthSamples.DynamicSchemes;
@@ -17,12 +17,15 @@ public class Program
1717
{
1818
public static void Main(string[] args)
1919
{
20-
CreateWebHostBuilder(args)
20+
CreateHostBuilder(args)
2121
.Build()
2222
.Run();
2323
}
2424

25-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
26-
WebHost.CreateDefaultBuilder(args)
27-
.UseStartup<Startup>();
25+
public static IHostBuilder CreateHostBuilder(string[] args) =>
26+
Host.CreateDefaultBuilder(args)
27+
.ConfigureWebHostDefaults(webBuilder =>
28+
{
29+
webBuilder.UseStartup<Startup>();
30+
});
2831
}

0 commit comments

Comments
 (0)