Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 31, 2025

Part of #20964, #27528, and #63017

Pattern transformation applied:

// Before (deprecated)
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

// After (recommended)
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Diagnostic IDs

Add ASPDEPR008 and ASPDEPR009. I grouped a bunch of APIs with ASPDEPR008, the ones that are closely related to WebHost and IWebHost. And ASPDEPR009 is for the Windows Service APIs that use IWebHost. 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 { }

@Copilot Copilot AI changed the title [WIP] Mark WebHost as obsolete Add [Obsolete] attribute to WebHost class Jul 31, 2025
@Copilot Copilot AI requested a review from BrennanConroy July 31, 2025 16:36
Copilot finished work on behalf of BrennanConroy July 31, 2025 16:36
@BrennanConroy
Copy link
Member

Copy link
Contributor Author

Copilot AI commented Jul 31, 2025

@copilot I meant https://github.com/dotnet/aspnetcore/blob/main/src/DefaultBuilder/src/WebHost.cs

You're absolutely right! I applied the [Obsolete] attribute to the correct WebHost class in /src/DefaultBuilder/src/WebHost.cs (the public static class that provides convenience methods) rather than the internal implementation. I've also reverted the changes to the wrong files and added appropriate pragma warning suppressions for test code and samples. All builds and tests are now passing. (commit 4c94381)

Copilot finished work on behalf of BrennanConroy July 31, 2025 17:18
@BrennanConroy
Copy link
Member

@copilot can you convert the sample apps to HostBuilder?
e.g.

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>();
            });

Copy link
Contributor Author

Copilot AI commented Jul 31, 2025

@copilot can you convert the sample apps to HostBuilder?
e.g.

...

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:

  • Security sample apps (8): Cookies, SocialSample, DynamicSchemes, PathSchemeSelection, StaticFilesAuth, CustomPolicyProvider, ClaimsTransformation, Identity.ExternalClaims
  • DefaultBuilder sample: SampleApp DirectWebHost method
  • Test assets (3): CreateDefaultBuilderApp, CreateDefaultBuilderOfTApp, DependencyInjectionApp
  • Components test asset: ComponentsApp.Server

Pattern applied:

  • Changed CreateWebHostBuilder to CreateHostBuilder
  • Changed IWebHostBuilder to IHostBuilder
  • Wrapped configuration in ConfigureWebHostDefaults(webBuilder => { ... })
  • Updated using statements to replace Microsoft.AspNetCore with Microsoft.Extensions.Hosting

All builds and tests pass successfully. (commit ac4fcb6)

@Copilot Copilot AI changed the title Add [Obsolete] attribute to WebHost class Add [Obsolete] attribute to WebHost class and convert sample apps to HostBuilder pattern Jul 31, 2025
Copilot finished work on behalf of BrennanConroy July 31, 2025 18:38
Copy link
Member

@BrennanConroy BrennanConroy left a 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

Copy link
Contributor Author

Copilot AI commented Jul 31, 2025

@copilot let's mark IWebHost as obsolete now

I've marked the IWebHost interface as obsolete with the message "IWebHost is obsolete. Use IHost instead." and added appropriate pragma warning suppressions to framework code that still needs to use it for backward compatibility. The obsolete attribute properly generates compiler warnings for consumers while maintaining compatibility. (commit 410ec79)

@Copilot Copilot AI changed the title Add [Obsolete] attribute to WebHost class and convert sample apps to HostBuilder pattern Add [Obsolete] attribute to WebHost class and IWebHost interface, convert sample apps to HostBuilder pattern Jul 31, 2025
@Copilot Copilot AI requested a review from BrennanConroy July 31, 2025 20:40
Copilot finished work on behalf of BrennanConroy July 31, 2025 20:40
@BrennanConroy
Copy link
Member

…nd update pragma disables

Co-authored-by: BrennanConroy <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 12, 2025

@copilot I updated one of the obsoletions to use ASPDEPR008 diagnostic code, could you update the appropriate pragma disables to use ASPDEPR008 instead of CS0618

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)

Copilot finished work on behalf of BrennanConroy August 12, 2025 21:41
Copy link
Member

@BrennanConroy BrennanConroy left a 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.

…agma disables, and add suppression to test projects

Co-authored-by: BrennanConroy <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 12, 2025

@copilot
Add disabling ASPDEPR008 to src/Servers/IIS/IIS/test/testassets/InProcessWebSite/Program.cs

...

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)

@Copilot Copilot AI changed the title Add [Obsolete] attribute to WebHost class and IWebHost interface, convert sample apps to HostBuilder pattern Add [Obsolete] attribute to WebHost class and IWebHost interface with proper diagnostic codes, convert sample apps to HostBuilder pattern Aug 12, 2025
@Copilot Copilot AI requested a review from BrennanConroy August 12, 2025 22:33
Copilot finished work on behalf of BrennanConroy August 12, 2025 22:33
@BrennanConroy BrennanConroy marked this pull request as ready for review August 14, 2025 17:39
@Copilot Copilot AI review requested due to automatic review settings August 14, 2025 17:39
@BrennanConroy BrennanConroy requested review from halter73, JamesNK and a team as code owners August 14, 2025 17:39
Copy link
Contributor

@Copilot Copilot AI left a 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 and ASPDEPR009 diagnostic codes to mark WebHost and related APIs as obsolete
  • Converts sample applications from WebHost.CreateDefaultBuilder to Host.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

@BrennanConroy BrennanConroy merged commit fe0ccd9 into main Aug 15, 2025
29 checks passed
@BrennanConroy BrennanConroy deleted the copilot/fix-8c74ddf5-60bc-415b-87f4-6cb997dfa74a branch August 15, 2025 17:33
@BrennanConroy BrennanConroy added this to the 10.0-rc1 milestone Aug 15, 2025
/// <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")]
Copy link
Member

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:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants