Skip to content

Commit 8c3f586

Browse files
Update WebHostBuilder usage to HostBuilder pattern in four target files
Co-authored-by: BrennanConroy <[email protected]>
1 parent 54f0cdd commit 8c3f586

File tree

4 files changed

+141
-100
lines changed
  • src

4 files changed

+141
-100
lines changed

src/DataProtection/DataProtection/test/Microsoft.AspNetCore.DataProtection.Tests/HostingTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ public async Task WebhostLoadsKeyRingBeforeServerStarts()
2525
.Returns(Mock.Of<IKeyRing>())
2626
.Callback(() => tcs.TrySetResult());
2727

28-
var builder = new WebHostBuilder()
29-
.UseStartup<TestStartup>()
28+
var builder = new HostBuilder()
29+
.ConfigureWebHost(webHostBuilder =>
30+
{
31+
webHostBuilder
32+
.UseStartup<TestStartup>();
33+
})
3034
.ConfigureServices(s =>
3135
s.AddDataProtection()
3236
.Services

src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Program.cs

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ public static int Main(string[] args)
5656
return 12;
5757
case "HangOnStop":
5858
{
59-
var host = new WebHostBuilder()
60-
.UseIIS()
61-
.UseStartup<Startup>()
59+
var host = new HostBuilder()
60+
.ConfigureWebHost(webHostBuilder =>
61+
{
62+
webHostBuilder
63+
.UseIIS()
64+
.UseStartup<Startup>();
65+
})
6266
.Build();
6367
host.Run();
6468

@@ -67,10 +71,14 @@ public static int Main(string[] args)
6771
break;
6872
case "IncreaseShutdownLimit":
6973
{
70-
var host = new WebHostBuilder()
71-
.UseIIS()
72-
.UseShutdownTimeout(TimeSpan.FromSeconds(120))
73-
.UseStartup<Startup>()
74+
var host = new HostBuilder()
75+
.ConfigureWebHost(webHostBuilder =>
76+
{
77+
webHostBuilder
78+
.UseIIS()
79+
.UseShutdownTimeout(TimeSpan.FromSeconds(120))
80+
.UseStartup<Startup>();
81+
})
7482
.Build();
7583

7684
host.Run();
@@ -94,11 +102,15 @@ public static int Main(string[] args)
94102
return 0;
95103
case "OverriddenServer":
96104
{
97-
var host = new WebHostBuilder()
98-
.UseIIS()
99-
.ConfigureServices(services => services.AddSingleton<IServer, DummyServer>())
100-
.Configure(builder => builder.Run(async context => { await context.Response.WriteAsync("I shouldn't work"); }))
101-
.Build();
105+
var host = new HostBuilder()
106+
.ConfigureWebHost(webHostBuilder =>
107+
{
108+
webHostBuilder
109+
.UseIIS()
110+
.ConfigureServices(services => services.AddSingleton<IServer, DummyServer>())
111+
.Configure(builder => builder.Run(async context => { await context.Response.WriteAsync("I shouldn't work"); }));
112+
})
113+
.Build();
102114
host.Run();
103115
}
104116
break;
@@ -111,18 +123,22 @@ public static int Main(string[] args)
111123
#if !FORWARDCOMPAT
112124
case "DecreaseRequestLimit":
113125
{
114-
var host = new WebHostBuilder()
126+
var host = new HostBuilder()
115127
.ConfigureLogging((_, factory) =>
116128
{
117129
factory.AddConsole();
118130
factory.AddFilter("Console", level => level >= LogLevel.Information);
119131
})
120-
.UseIIS()
121-
.ConfigureServices(services =>
132+
.ConfigureWebHost(webHostBuilder =>
122133
{
123-
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = 2);
134+
webHostBuilder
135+
.UseIIS()
136+
.ConfigureServices(services =>
137+
{
138+
services.Configure<IISServerOptions>(options => options.MaxRequestBodySize = 2);
139+
})
140+
.UseStartup<Startup>();
124141
})
125-
.UseStartup<Startup>()
126142
.Build();
127143

128144
host.Run();
@@ -131,15 +147,19 @@ public static int Main(string[] args)
131147
#endif
132148
case "ThrowInStartup":
133149
{
134-
var host = new WebHostBuilder()
135-
.ConfigureLogging((_, factory) =>
136-
{
137-
factory.AddConsole();
138-
factory.AddFilter("Console", level => level >= LogLevel.Information);
139-
})
140-
.UseIIS()
141-
.UseStartup<ThrowingStartup>()
142-
.Build();
150+
var host = new HostBuilder()
151+
.ConfigureLogging((_, factory) =>
152+
{
153+
factory.AddConsole();
154+
factory.AddFilter("Console", level => level >= LogLevel.Information);
155+
})
156+
.ConfigureWebHost(webHostBuilder =>
157+
{
158+
webHostBuilder
159+
.UseIIS()
160+
.UseStartup<ThrowingStartup>();
161+
})
162+
.Build();
143163

144164
host.Run();
145165
}
@@ -189,16 +209,20 @@ public static int Main(string[] args)
189209

190210
private static int StartServer()
191211
{
192-
var host = new WebHostBuilder()
212+
var host = new HostBuilder()
193213
.ConfigureLogging((_, factory) =>
194214
{
195215
factory.AddConsole();
196216
factory.AddFilter("Console", level => level >= LogLevel.Information);
197217
})
198-
.UseKestrel()
199-
.UseIIS()
200-
.UseIISIntegration()
201-
.UseStartup<Startup>()
218+
.ConfigureWebHost(webHostBuilder =>
219+
{
220+
webHostBuilder
221+
.UseKestrel()
222+
.UseIIS()
223+
.UseIISIntegration()
224+
.UseStartup<Startup>();
225+
})
202226
.Build();
203227

204228
host.Run();

src/Servers/Kestrel/test/Interop.FunctionalTests/Http3/Http3TlsTests.cs

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -345,39 +345,43 @@ public async Task TlsHandshakeCallbackOptions_Invoked()
345345
[InlineData(false, false, false)]
346346
public async Task UseKestrelCore_CodeBased(bool useQuic, bool useHttps, bool useHttpsEnablesHttpsConfiguration)
347347
{
348-
var hostBuilder = new WebHostBuilder()
349-
.UseKestrelCore()
350-
.ConfigureKestrel(serverOptions =>
351-
{
352-
serverOptions.ListenAnyIP(0, listenOptions =>
348+
var hostBuilder = new HostBuilder()
349+
.ConfigureWebHost(webHostBuilder =>
350+
{
351+
webHostBuilder
352+
.UseKestrelCore()
353+
.ConfigureKestrel(serverOptions =>
353354
{
354-
listenOptions.Protocols = HttpProtocols.Http3;
355-
if (useHttps)
355+
serverOptions.ListenAnyIP(0, listenOptions =>
356356
{
357-
if (useHttpsEnablesHttpsConfiguration)
357+
listenOptions.Protocols = HttpProtocols.Http3;
358+
if (useHttps)
358359
{
359-
listenOptions.UseHttps(httpsOptions =>
360+
if (useHttpsEnablesHttpsConfiguration)
360361
{
361-
httpsOptions.ServerCertificate = TestResources.GetTestCertificate();
362-
});
363-
}
364-
else
365-
{
366-
// Specifically choose an overload that doesn't enable https configuration
367-
listenOptions.UseHttps(new HttpsConnectionAdapterOptions
362+
listenOptions.UseHttps(httpsOptions =>
363+
{
364+
httpsOptions.ServerCertificate = TestResources.GetTestCertificate();
365+
});
366+
}
367+
else
368368
{
369-
ServerCertificate = TestResources.GetTestCertificate()
370-
});
369+
// Specifically choose an overload that doesn't enable https configuration
370+
listenOptions.UseHttps(new HttpsConnectionAdapterOptions
371+
{
372+
ServerCertificate = TestResources.GetTestCertificate()
373+
});
374+
}
371375
}
372-
}
373-
});
374-
})
375-
.Configure(app => { });
376+
});
377+
})
378+
.Configure(app => { });
376379

377-
if (useQuic)
378-
{
379-
hostBuilder.UseQuic();
380-
}
380+
if (useQuic)
381+
{
382+
webHostBuilder.UseQuic();
383+
}
384+
});
381385

382386
var host = hostBuilder.Build();
383387

@@ -400,25 +404,29 @@ public async Task UseKestrelCore_CodeBased(bool useQuic, bool useHttps, bool use
400404
[InlineData(false)]
401405
public void UseKestrelCore_ConfigurationBased(bool useQuic)
402406
{
403-
var hostBuilder = new WebHostBuilder()
404-
.UseKestrelCore()
405-
.ConfigureKestrel(serverOptions =>
406-
{
407-
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
407+
var hostBuilder = new HostBuilder()
408+
.ConfigureWebHost(webHostBuilder =>
409+
{
410+
webHostBuilder
411+
.UseKestrelCore()
412+
.ConfigureKestrel(serverOptions =>
408413
{
409-
new KeyValuePair<string, string>("Endpoints:end1:Url", "https://127.0.0.1:0"),
410-
new KeyValuePair<string, string>("Endpoints:end1:Protocols", "Http3"),
411-
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
412-
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
413-
}).Build();
414-
serverOptions.Configure(config);
415-
})
416-
.Configure(app => { });
417-
418-
if (useQuic)
419-
{
420-
hostBuilder.UseQuic();
421-
}
414+
var config = new ConfigurationBuilder().AddInMemoryCollection(new[]
415+
{
416+
new KeyValuePair<string, string>("Endpoints:end1:Url", "https://127.0.0.1:0"),
417+
new KeyValuePair<string, string>("Endpoints:end1:Protocols", "Http3"),
418+
new KeyValuePair<string, string>("Certificates:Default:Path", Path.Combine("shared", "TestCertificates", "aspnetdevcert.pfx")),
419+
new KeyValuePair<string, string>("Certificates:Default:Password", "testPassword"),
420+
}).Build();
421+
serverOptions.Configure(config);
422+
})
423+
.Configure(app => { });
424+
425+
if (useQuic)
426+
{
427+
webHostBuilder.UseQuic();
428+
}
429+
});
422430

423431
var host = hostBuilder.Build();
424432

src/Servers/testassets/ServerComparison.TestSites/Program.cs

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,43 @@ public static class Program
1313
{
1414
public static void Main(string[] args)
1515
{
16-
var builder = new WebHostBuilder()
17-
.UseConfiguration(new ConfigurationBuilder()
18-
.AddCommandLine(args)
19-
.Build())
16+
var configuration = new ConfigurationBuilder()
17+
.AddCommandLine(args)
18+
.Build();
19+
20+
var builder = new HostBuilder()
21+
.UseConfiguration(configuration)
2022
.ConfigureLogging((_, factory) =>
2123
{
2224
factory.AddConsole();
2325
factory.AddFilter("Console", level => level >= LogLevel.Warning);
2426
})
25-
.UseStartup("ServerComparison.TestSites");
26-
27-
builder.UseKestrel();
28-
builder.UseIISIntegration();
29-
builder.UseIIS();
30-
31-
// Switch between Kestrel, IIS, and HttpSys for different tests. Default to Kestrel for normal app execution.
32-
if (string.Equals(builder.GetSetting("server"), "Microsoft.AspNetCore.Server.HttpSys", StringComparison.OrdinalIgnoreCase))
33-
{
34-
builder.UseHttpSys(options =>
27+
.ConfigureWebHost(webHostBuilder =>
3528
{
36-
if (string.Equals(builder.GetSetting("environment") ??
37-
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"),
38-
"NtlmAuthentication", StringComparison.OrdinalIgnoreCase))
29+
webHostBuilder
30+
.UseStartup("ServerComparison.TestSites")
31+
.UseKestrel()
32+
.UseIISIntegration()
33+
.UseIIS();
34+
35+
// Switch between Kestrel, IIS, and HttpSys for different tests. Default to Kestrel for normal app execution.
36+
if (string.Equals(configuration["server"], "Microsoft.AspNetCore.Server.HttpSys", StringComparison.OrdinalIgnoreCase))
3937
{
40-
// Set up NTLM authentication for HttpSys as follows.
41-
// For IIS and IISExpress use inetmgr to setup NTLM authentication on the application or
42-
// modify the applicationHost.config to enable NTLM.
43-
options.Authentication.AllowAnonymous = true;
44-
options.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
38+
webHostBuilder.UseHttpSys(options =>
39+
{
40+
if (string.Equals(configuration["environment"] ??
41+
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"),
42+
"NtlmAuthentication", StringComparison.OrdinalIgnoreCase))
43+
{
44+
// Set up NTLM authentication for HttpSys as follows.
45+
// For IIS and IISExpress use inetmgr to setup NTLM authentication on the application or
46+
// modify the applicationHost.config to enable NTLM.
47+
options.Authentication.AllowAnonymous = true;
48+
options.Authentication.Schemes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM;
49+
}
50+
});
4551
}
4652
});
47-
}
4853

4954
var host = builder.Build();
5055

0 commit comments

Comments
 (0)