Skip to content

Mark WebHostBuilder class as obsolete #62785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5102b25
Initial plan
Copilot Jul 17, 2025
54f0cdd
Mark WebHostBuilder class as obsolete with placeholder message
Copilot Jul 17, 2025
8c3f586
Update WebHostBuilder usage to HostBuilder pattern in four target files
Copilot Jul 17, 2025
8869208
fixup
BrennanConroy Jul 17, 2025
5407fb0
Address review feedback: Add pragma disables and update to HostBuilde…
Copilot Jul 17, 2025
dd80d9c
Add pragma warning suppression for WebHost.cs and remove from WebHost…
Copilot Jul 17, 2025
3f16853
Update TestServerTests.cs and HttpsConfigurationTests.cs to use HostB…
Copilot Jul 17, 2025
d028735
test
BrennanConroy Jul 17, 2025
95e6eaa
fix tests
BrennanConroy Jul 18, 2025
194d366
Remove nowarn settings and start converting TestHost tests to HostBui…
Copilot Jul 18, 2025
e87e383
Complete conversion of RequestBuilderTests, ClientHandlerTests, and W…
Copilot Jul 18, 2025
bd4d52f
Start converting TestHost test files to HostBuilder pattern
Copilot Jul 19, 2025
b146f4d
Complete conversion of HttpContextBuilderTests.cs to HostBuilder pattern
Copilot Jul 19, 2025
2f609b9
Partial conversion of TestClientTests.cs and TestServerTests.cs to Ho…
Copilot Jul 19, 2025
dfcad52
Complete TestHost test file WebHostBuilder conversion - added pragma …
Copilot Jul 20, 2025
9134e94
startasync
BrennanConroy Jul 21, 2025
f6c7ee3
convert
BrennanConroy Jul 21, 2025
161a1b5
Convert 12 more TestServerTests to HostBuilder pattern
Copilot Jul 22, 2025
5279775
testservertests
BrennanConroy Jul 23, 2025
ac08998
Update WebHostBuilder obsolete attribute to use ASPDEPR004 diagnostic ID
Copilot Jul 24, 2025
d63a4a8
Apply suggestions from code review
BrennanConroy Jul 24, 2025
c151d24
Update src/Hosting/TestHost/test/TestClientTests.cs
BrennanConroy Jul 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public async Task WebhostLoadsKeyRingBeforeServerStarts()
.Returns(Mock.Of<IKeyRing>())
.Callback(() => tcs.TrySetResult());

#pragma warning disable ASPDEPR004 // Type or member is obsolete
var builder = new WebHostBuilder()
.UseStartup<TestStartup>()
.ConfigureServices(s =>
Expand All @@ -33,6 +34,7 @@ public async Task WebhostLoadsKeyRingBeforeServerStarts()
.Replace(ServiceDescriptor.Singleton(mockKeyRing.Object))
.AddSingleton<IServer>(
new FakeServer(onStart: () => tcs.TrySetException(new InvalidOperationException("Server was started before key ring was initialized")))));
#pragma warning restore ASPDEPR004 // Type or member is obsolete

using (var host = builder.Build())
{
Expand Down
2 changes: 2 additions & 0 deletions src/DefaultBuilder/src/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ public static IWebHostBuilder CreateDefaultBuilder() =>
/// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
#pragma warning disable ASPDEPR004 // Type or member is obsolete
var builder = new WebHostBuilder();
#pragma warning restore ASPDEPR004 // Type or member is obsolete

if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
{
Expand Down
1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/WebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.Hosting;
/// <summary>
/// A builder for <see cref="IWebHost"/>
/// </summary>
[Obsolete("WebHostBuilder is deprecated in favor of HostBuilder and WebApplicationBuilder. For more information, visit https://aka.ms/aspnet/deprecate/004.", DiagnosticId = "ASPDEPR004")]
public class WebHostBuilder : IWebHostBuilder
{
private readonly HostingEnvironment _hostingEnvironment;
Expand Down
8 changes: 8 additions & 0 deletions src/Hosting/Hosting/test/WebHostBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public void DoNotCaptureStartupErrorsByDefault(IWebHostBuilder builder)
public void ServiceProviderDisposedOnBuildException()
{
var service = new DisposableService();
#pragma warning disable ASPDEPR004 // Type or member is obsolete
var hostBuilder = new WebHostBuilder()
.UseServer(new TestServer())
.ConfigureServices(services =>
Expand All @@ -437,6 +438,7 @@ public void ServiceProviderDisposedOnBuildException()
services.AddSingleton(sp => service);
})
.UseStartup<StartupWithResolvedDisposableThatThrows>();
#pragma warning restore ASPDEPR004 // Type or member is obsolete

Assert.Throws<InvalidOperationException>(() => hostBuilder.Build());
Assert.True(service.Disposed);
Expand Down Expand Up @@ -1483,14 +1485,18 @@ private IWebHostBuilder CreateWebHostBuilder()
.AddInMemoryCollection(vals);
var config = builder.Build();

#pragma warning disable ASPDEPR004 // Type or member is obsolete
return new WebHostBuilder().UseConfiguration(config);
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

#pragma warning disable ASPDEPR004 // Type or member is obsolete
public static TheoryData<IWebHostBuilder> DefaultWebHostBuilders => new TheoryData<IWebHostBuilder>
{
new WebHostBuilder(),
new GenericWebHostBuilderWrapper(new HostBuilder())
};
#pragma warning restore ASPDEPR004 // Type or member is obsolete

public static TheoryData<IWebHostBuilder> DefaultWebHostBuildersWithConfig
{
Expand All @@ -1506,10 +1512,12 @@ public static TheoryData<IWebHostBuilder> DefaultWebHostBuildersWithConfig
.AddInMemoryCollection(vals);
var config = builder.Build();

#pragma warning disable ASPDEPR004 // Type or member is obsolete
return new TheoryData<IWebHostBuilder> {
new WebHostBuilder().UseConfiguration(config),
new GenericWebHostBuilderWrapper(new HostBuilder()).UseConfiguration(config)
};
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Hosting/Hosting/test/WebHostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,9 @@ private IWebHost CreateHost(RequestDelegate requestDelegate)

private IWebHostBuilder CreateBuilder(IConfiguration config = null)
{
#pragma warning disable ASPDEPR004 // Type or member is obsolete
return new WebHostBuilder().UseConfiguration(config ?? new ConfigurationBuilder().Build()).UseStartup("Microsoft.AspNetCore.Hosting.Tests");
#pragma warning restore ASPDEPR004 // Type or member is obsolete
}

private static bool[] RegisterCallbacksThatThrow(IServiceCollection services)
Expand Down
31 changes: 20 additions & 11 deletions src/Hosting/TestHost/test/ClientHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.InternalTesting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;

Expand Down Expand Up @@ -691,19 +692,27 @@ public async Task ClientHandlerCreateContextWithDefaultRequestParameters()
{
// This logger will attempt to access information from HttpRequest once the HttpContext is created
var logger = new VerifierLogger();
var builder = new WebHostBuilder()
.ConfigureServices(services =>
using var host = new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
services.AddSingleton<ILogger<IWebHost>>(logger);
webHostBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddSingleton<ILogger<IWebHost>>(logger);
})
.Configure(app =>
{
app.Run(context =>
{
return Task.FromResult(0);
});
});
})
.Configure(app =>
{
app.Run(context =>
{
return Task.FromResult(0);
});
});
var server = new TestServer(builder);
.Build();
var server = host.GetTestServer();

await host.StartAsync();

// The HttpContext will be created and the logger will make sure that the HttpRequest exists and contains reasonable values
var result = await server.CreateClient().GetStringAsync("/");
Expand Down
Loading
Loading