-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add [Obsolete] attribute to WebHost class and IWebHost interface with proper diagnostic codes, convert sample apps to HostBuilder pattern #63024
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
Add [Obsolete] attribute to WebHost class and IWebHost interface with proper diagnostic codes, convert sample apps to HostBuilder pattern #63024
Conversation
Co-authored-by: BrennanConroy <[email protected]>
…warning suppressions Co-authored-by: BrennanConroy <[email protected]>
You're absolutely right! I applied the [Obsolete] attribute to the correct WebHost class in |
@copilot can you convert the sample apps to HostBuilder? Before: public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>(); After: public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
}); |
Co-authored-by: BrennanConroy <[email protected]>
... I've converted 12 sample apps from the WebHost.CreateDefaultBuilder pattern to the Host.CreateDefaultBuilder pattern as requested. The conversion follows the exact pattern you provided: Converted files:
Pattern applied:
All builds and tests pass successfully. (commit ac4fcb6) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot let's mark IWebHost as obsolete now
src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs
Outdated
Show resolved
Hide resolved
src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs
Outdated
Show resolved
Hide resolved
src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: BrennanConroy <[email protected]>
I've marked the |
…nd update pragma disables Co-authored-by: BrennanConroy <[email protected]>
Updated the WebHost obsolete attribute to use ASPDEPR008 diagnostic code and replaced the appropriate CS0618 pragma disables with ASPDEPR008 in files that use WebHost methods. All builds and tests pass successfully. (commit fc1770f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
Add disabling ASPDEPR008 to src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Program.cs
Also, update anymore warning disables with the rest of the diagnostic ID changes.
src/Servers/Kestrel/shared/test/TransportTestHelpers/IWebHostPortExtensions.cs
Outdated
Show resolved
Hide resolved
src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs
Outdated
Show resolved
Hide resolved
…agma disables, and add suppression to test projects Co-authored-by: BrennanConroy <[email protected]>
... Added ASPDEPR008 suppression to InProcessWebSite/Program.cs and updated all pragma disables to use the correct diagnostic IDs: ASPDEPR008 for WebHost/IWebHost related obsoletions and ASPDEPR004 for WindowsService related obsoletions. Also added project-level warning suppressions for test projects that legitimately test obsolete functionality. (commit 650081c) |
src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds obsolete attributes to WebHost class and IWebHost interface with proper diagnostic codes, and converts sample apps to use the HostBuilder pattern. The goal is to deprecate the legacy WebHost pattern in favor of the more modern Host.CreateDefaultBuilder approach.
- Adds
ASPDEPR008
andASPDEPR009
diagnostic codes to mark WebHost and related APIs as obsolete - Converts sample applications from
WebHost.CreateDefaultBuilder
toHost.CreateDefaultBuilder
pattern - Adds pragma warning suppressions to test code that still needs to use deprecated APIs
Reviewed Changes
Copilot reviewed 53 out of 53 changed files in this pull request and generated no comments.
File | Description |
---|---|
Various sample Program.cs files |
Converted from WebHost pattern to Host.CreateDefaultBuilder pattern |
Test files | Added pragma warning suppressions for ASPDEPR008 to continue testing deprecated APIs |
WebHost-related source files | Added Obsolete attributes with appropriate diagnostic IDs |
WebApplicationFactory.cs | Added obsolete attributes to methods using IWebHost |
src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Safia Abdalla <[email protected]>
/// <summary> | ||
/// Provides convenience methods for creating instances of <see cref="IWebHost"/> and <see cref="IWebHostBuilder"/> with pre-configured defaults. | ||
/// </summary> | ||
[Obsolete("WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead. For more information, visit https://aka.ms/aspnet/deprecate/008.", DiagnosticId = "ASPDEPR008")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BrennanConroy Maybe it's just because it's a TODO that hasn't been actioned yet, but if I visit this link I get this message:

Part of #20964, #27528, and #63017
Pattern transformation applied:
Diagnostic IDs
Add
ASPDEPR008
andASPDEPR009
. I grouped a bunch of APIs withASPDEPR008
, the ones that are closely related toWebHost
andIWebHost
. AndASPDEPR009
is for the Windows Service APIs that useIWebHost
. Listed all the APIs below and the specific obsoletion messages used.ASPDEPR008
"IWebHost is obsolete. Use IHost instead. For more information, visit https://aka.ms/aspnet/deprecate/008.":
public interface IWebHost { }
IWebHost IWebHostBuilder.Build();
public IWebHost TestServer.Host { get; }
public static TestServer WebHostBuilderExtensions.GetTestServer(this IWebHost host) { }
public static HttpClient WebHostBuilderExtensions.GetTestClient(this IWebHost host) { }
"WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead. For more information, visit https://aka.ms/aspnet/deprecate/008.":
public static class WebHost { }
"WebHostExtensions is obsolete. Use Host.CreateDefaultBuilder or WebApplication.CreateBuilder instead. For more information, visit https://aka.ms/aspnet/deprecate/008.":
public static class WebHostExtensions { }
"IWebHost, which this method uses, is obsolete. Use one of the ctors that takes an IServiceProvider instead. For more information, visit https://aka.ms/aspnet/deprecate/008.":
public TestServer(IWebHostBuilder builder) { }
public TestServer(IWebHostBuilder builder, IFeatureCollection featureCollection) { }
"WebHost, which this method uses, is obsolete. Use one of the overloads that takes an IServiceProvider instead. For more information, visit https://aka.ms/aspnet/deprecate/008.":
protected virtual TestServer WebApplicationFactory.CreateServer(IWebHostBuilder builder) { }
ASPDEPR009
"Use UseWindowsService and AddHostedService instead. For more information, visit https://aka.ms/aspnet/deprecate/009.":
public class WebHostService { }
public static class WebHostWindowsServiceExtensions { }