Skip to content

Add [Obsolete] attribute to WebHost class and IWebHost interface, 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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 31, 2025

This PR adds the [Obsolete] attribute to both the WebHost class and IWebHost interface in the ASP.NET Core hosting abstractions to indicate that they are obsolete and guide developers toward using HostBuilder or WebApplicationBuilder instead. Additionally, it converts sample applications from the deprecated WebHost pattern to the recommended HostBuilder pattern.

Changes

WebHost and IWebHost Obsolescence:

  • WebHost.cs: Added [Obsolete("WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead.")] attribute to the public static WebHost class declaration
  • IWebHost.cs: Added [Obsolete("IWebHost is obsolete. Use IHost instead.")] attribute to the IWebHost interface declaration
  • Framework code: Added pragma warning suppressions (#pragma warning disable CS0618) to 15+ internal framework files that need to continue using these obsolete APIs for backward compatibility, including:
    • GenericHostBuilderExtensions.cs and WebApplicationBuilder.cs (existing suppressions)
    • Hosting abstractions, implementations, and test infrastructure
    • TestHost, WebApplicationFactory, and Windows Services integration
  • Test assets: Removed unnecessary pragma warning suppressions from 3 DefaultBuilder test assets that were converted to modern HostBuilder pattern

Sample App Modernization:

  • 12 sample apps converted from WebHost.CreateDefaultBuilder pattern to Host.CreateDefaultBuilder pattern:
    • Security samples: Cookies, SocialSample, DynamicSchemes, PathSchemeSelection, StaticFilesAuth, CustomPolicyProvider, ClaimsTransformation, Identity.ExternalClaims
    • DefaultBuilder sample: SampleApp DirectWebHost method
    • Test assets: CreateDefaultBuilderApp, CreateDefaultBuilderOfTApp, DependencyInjectionApp
    • Components test asset: ComponentsApp.Server

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

Verification

  • All existing builds pass without warnings or errors
  • All existing tests continue to pass
  • The obsolete attributes correctly generate compiler warnings for consumers:
    • warning CS0618: 'WebHost' is obsolete: 'WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead.'
    • warning CS0618: 'IWebHost' is obsolete: 'IWebHost is obsolete. Use IHost instead.'
  • Internal framework code with pragma suppressions continues to work without warnings
  • Converted sample apps demonstrate the modern hosting pattern while maintaining identical functionality

This change provides clear migration guidance to developers while maintaining backward compatibility for existing code that relies on the WebHost and IWebHost implementations.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@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

Comment on lines 4 to 5
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary #pragma warning disable CS0618 suppression since this test asset now uses the modern HostBuilder pattern and no longer needs to suppress obsolete warnings. (commit 410ec79)

Comment on lines 4 to 5
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary #pragma warning disable CS0618 suppression since this test asset now uses the modern HostBuilder pattern and no longer needs to suppress obsolete warnings. (commit 410ec79)

Comment on lines 4 to 5
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#pragma warning disable CS0618 // Type or member is obsolete

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the unnecessary #pragma warning disable CS0618 suppression since this test asset now uses the modern HostBuilder pattern and no longer needs to suppress obsolete warnings. (commit 410ec79)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants