From 2955f0e5d2e16669f2808c90062f02c4643c5e18 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 31 Jul 2025 16:24:59 +0000 Subject: [PATCH 1/7] Initial plan From 2711b5cb99de93143a4a6577ce386cac2d52e89d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 31 Jul 2025 16:35:25 +0000 Subject: [PATCH 2/7] Add [Obsolete] attribute to WebHost class with suppression pragmas Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- src/Hosting/Hosting/src/Internal/WebHost.cs | 1 + src/Hosting/Hosting/src/WebHostBuilder.cs | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index 6626cdda9889..23cf13b82aa0 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -19,6 +19,7 @@ namespace Microsoft.AspNetCore.Hosting; +[Obsolete("WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead.")] internal sealed partial class WebHost : IWebHost, IAsyncDisposable { private const string DeprecatedServerUrlsKey = "server.urls"; diff --git a/src/Hosting/Hosting/src/WebHostBuilder.cs b/src/Hosting/Hosting/src/WebHostBuilder.cs index 23d57a3e59ac..7e965e671959 100644 --- a/src/Hosting/Hosting/src/WebHostBuilder.cs +++ b/src/Hosting/Hosting/src/WebHostBuilder.cs @@ -160,12 +160,14 @@ public IWebHost Build() AddApplicationServices(applicationServices, hostingServiceProvider); +#pragma warning disable CS0618 // Type or member is obsolete var host = new WebHost( applicationServices, hostingServiceProvider, _options, _config, hostingStartupErrors); +#pragma warning restore CS0618 // Type or member is obsolete try { host.Initialize(); From 4c94381f4cf2c26179babdbc70b6874cf9ce78d3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 31 Jul 2025 17:16:53 +0000 Subject: [PATCH 3/7] Apply [Obsolete] attribute to correct WebHost class with appropriate warning suppressions Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- src/DefaultBuilder/samples/SampleApp/Program.cs | 2 ++ src/DefaultBuilder/src/GenericHostBuilderExtensions.cs | 2 ++ src/DefaultBuilder/src/WebApplicationBuilder.cs | 2 ++ src/DefaultBuilder/src/WebHost.cs | 1 + .../WebHostFunctionalTests.cs | 2 ++ .../test/Microsoft.AspNetCore.Tests/WebHostTests.cs | 2 ++ .../testassets/CreateDefaultBuilderApp/Program.cs | 2 ++ .../testassets/CreateDefaultBuilderOfTApp/Program.cs | 2 ++ .../testassets/DependencyInjectionApp/Program.cs | 4 +++- .../testassets/StartRequestDelegateUrlApp/Program.cs | 2 ++ .../testassets/StartRouteBuilderUrlApp/Program.cs | 2 ++ .../testassets/StartWithIApplicationBuilderUrlApp/Program.cs | 2 ++ src/Hosting/Hosting/src/Internal/WebHost.cs | 1 - src/Hosting/Hosting/src/WebHostBuilder.cs | 2 -- 14 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/DefaultBuilder/samples/SampleApp/Program.cs b/src/DefaultBuilder/samples/SampleApp/Program.cs index fa29b18ac461..fcb6d2260037 100644 --- a/src/DefaultBuilder/samples/SampleApp/Program.cs +++ b/src/DefaultBuilder/samples/SampleApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.AspNetCore; namespace SampleApp; diff --git a/src/DefaultBuilder/src/GenericHostBuilderExtensions.cs b/src/DefaultBuilder/src/GenericHostBuilderExtensions.cs index f61d482b4066..483fcc34568b 100644 --- a/src/DefaultBuilder/src/GenericHostBuilderExtensions.cs +++ b/src/DefaultBuilder/src/GenericHostBuilderExtensions.cs @@ -61,7 +61,9 @@ public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder, A return builder.ConfigureWebHost(webHostBuilder => { +#pragma warning disable CS0618 // Type or member is obsolete WebHost.ConfigureWebDefaults(webHostBuilder); +#pragma warning restore CS0618 // Type or member is obsolete configure(webHostBuilder); }, configureOptions); diff --git a/src/DefaultBuilder/src/WebApplicationBuilder.cs b/src/DefaultBuilder/src/WebApplicationBuilder.cs index 2cff4ae4ffb9..409e724eee63 100644 --- a/src/DefaultBuilder/src/WebApplicationBuilder.cs +++ b/src/DefaultBuilder/src/WebApplicationBuilder.cs @@ -127,7 +127,9 @@ internal WebApplicationBuilder(WebApplicationOptions options, bool slim, Action< bootstrapHostBuilder.ConfigureSlimWebHost( webHostBuilder => { +#pragma warning disable CS0618 // Type or member is obsolete AspNetCore.WebHost.ConfigureWebDefaultsSlim(webHostBuilder); +#pragma warning restore CS0618 // Type or member is obsolete // Runs inline. webHostBuilder.Configure(ConfigureApplication); diff --git a/src/DefaultBuilder/src/WebHost.cs b/src/DefaultBuilder/src/WebHost.cs index 1c3b9b4f07fe..ad14b7e93485 100644 --- a/src/DefaultBuilder/src/WebHost.cs +++ b/src/DefaultBuilder/src/WebHost.cs @@ -21,6 +21,7 @@ namespace Microsoft.AspNetCore; /// /// Provides convenience methods for creating instances of and with pre-configured defaults. /// +[Obsolete("WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead.")] public static class WebHost { /// diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs index 94a64ffc74fc..52155915c0fe 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.FunctionalTests/WebHostFunctionalTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Net.Http; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.IntegrationTesting; diff --git a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs index 8205e1459a08..aff27723a8b0 100644 --- a/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs +++ b/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebHostTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Collections.Concurrent; using System.Diagnostics.Tracing; using Microsoft.AspNetCore.Builder; diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs index 947c13abb87d..78b4ea1186bb 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs index e5a9e22f8e23..94c794aca845 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; diff --git a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs index 657b7940895d..1cd83cc8fcb3 100644 --- a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs +++ b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs @@ -1,6 +1,8 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; diff --git a/src/DefaultBuilder/testassets/StartRequestDelegateUrlApp/Program.cs b/src/DefaultBuilder/testassets/StartRequestDelegateUrlApp/Program.cs index f2cadf086c50..3ba489e9a68b 100644 --- a/src/DefaultBuilder/testassets/StartRequestDelegateUrlApp/Program.cs +++ b/src/DefaultBuilder/testassets/StartRequestDelegateUrlApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using System.Threading; using Microsoft.AspNetCore; diff --git a/src/DefaultBuilder/testassets/StartRouteBuilderUrlApp/Program.cs b/src/DefaultBuilder/testassets/StartRouteBuilderUrlApp/Program.cs index 46980d54399d..522d4ad4e1c3 100644 --- a/src/DefaultBuilder/testassets/StartRouteBuilderUrlApp/Program.cs +++ b/src/DefaultBuilder/testassets/StartRouteBuilderUrlApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using System.Threading; using Microsoft.AspNetCore; diff --git a/src/DefaultBuilder/testassets/StartWithIApplicationBuilderUrlApp/Program.cs b/src/DefaultBuilder/testassets/StartWithIApplicationBuilderUrlApp/Program.cs index c3422885bc75..2391f76b1da0 100644 --- a/src/DefaultBuilder/testassets/StartWithIApplicationBuilderUrlApp/Program.cs +++ b/src/DefaultBuilder/testassets/StartWithIApplicationBuilderUrlApp/Program.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using System.Threading; using Microsoft.AspNetCore; diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index 23cf13b82aa0..6626cdda9889 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -19,7 +19,6 @@ namespace Microsoft.AspNetCore.Hosting; -[Obsolete("WebHost is obsolete. Use HostBuilder or WebApplicationBuilder instead.")] internal sealed partial class WebHost : IWebHost, IAsyncDisposable { private const string DeprecatedServerUrlsKey = "server.urls"; diff --git a/src/Hosting/Hosting/src/WebHostBuilder.cs b/src/Hosting/Hosting/src/WebHostBuilder.cs index 7e965e671959..23d57a3e59ac 100644 --- a/src/Hosting/Hosting/src/WebHostBuilder.cs +++ b/src/Hosting/Hosting/src/WebHostBuilder.cs @@ -160,14 +160,12 @@ public IWebHost Build() AddApplicationServices(applicationServices, hostingServiceProvider); -#pragma warning disable CS0618 // Type or member is obsolete var host = new WebHost( applicationServices, hostingServiceProvider, _options, _config, hostingStartupErrors); -#pragma warning restore CS0618 // Type or member is obsolete try { host.Initialize(); From ac4fcb6d5246db0c0128fdbae5a4ebd0a43c58d1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 31 Jul 2025 18:36:56 +0000 Subject: [PATCH 4/7] Convert sample apps from WebHost to HostBuilder pattern Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- .../ComponentsApp.Server/Program.cs | 16 +++--- .../samples/SampleApp/Program.cs | 9 ++-- .../CreateDefaultBuilderApp/Program.cs | 38 +++++++------- .../CreateDefaultBuilderOfTApp/Program.cs | 51 ++++++++++--------- .../DependencyInjectionApp/Program.cs | 42 ++++++++------- .../samples/SocialSample/Program.cs | 9 ++-- .../samples/ClaimsTransformation/Program.cs | 13 +++-- src/Security/samples/Cookies/Program.cs | 13 +++-- .../samples/CustomPolicyProvider/Program.cs | 13 +++-- .../samples/DynamicSchemes/Program.cs | 13 +++-- .../Identity.ExternalClaims/Program.cs | 13 +++-- .../samples/PathSchemeSelection/Program.cs | 13 +++-- .../samples/StaticFilesAuth/Program.cs | 13 +++-- 13 files changed, 149 insertions(+), 107 deletions(-) diff --git a/src/Components/test/testassets/ComponentsApp.Server/Program.cs b/src/Components/test/testassets/ComponentsApp.Server/Program.cs index 1fb78e81f354..93f34b504d12 100644 --- a/src/Components/test/testassets/ComponentsApp.Server/Program.cs +++ b/src/Components/test/testassets/ComponentsApp.Server/Program.cs @@ -1,7 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.AspNetCore; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace ComponentsApp.Server; @@ -12,10 +13,13 @@ public static void Main(string[] args) BuildWebHost(args).Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); - public static IWebHost BuildWebHost(string[] args) => - CreateWebHostBuilder(args).Build(); + public static IHost BuildWebHost(string[] args) => + CreateHostBuilder(args).Build(); } diff --git a/src/DefaultBuilder/samples/SampleApp/Program.cs b/src/DefaultBuilder/samples/SampleApp/Program.cs index fcb6d2260037..6b30b6c4c8f7 100644 --- a/src/DefaultBuilder/samples/SampleApp/Program.cs +++ b/src/DefaultBuilder/samples/SampleApp/Program.cs @@ -75,9 +75,12 @@ private static void CustomApplicationBuilder() } private static void DirectWebHost(string[] args) { - // Using defaults with a Startup class - using (var host = WebHost.CreateDefaultBuilder(args) - .UseStartup() + // Using defaults with HostBuilder pattern + using (var host = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) .Build()) { host.Run(); diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs index 78b4ea1186bb..36675cedf4d9 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs @@ -4,11 +4,11 @@ #pragma warning disable CS0618 // Type or member is obsolete using System; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace CreateDefaultBuilderApp; @@ -18,23 +18,27 @@ static void Main(string[] args) { string responseMessage = null; - WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" }) - .ConfigureServices((context, services) => responseMessage = responseMessage ?? GetResponseMessage(context)) - .ConfigureKestrel(options => options - .Configure(options.ConfigurationLoader.Configuration) - .Endpoint("HTTP", endpointOptions => - { - if (responseMessage == null - && !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"])) - { - responseMessage = "Default Kestrel configuration not read."; - } - })) - .Configure(app => app.Run(context => + Host.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" }) + .ConfigureWebHostDefaults(webBuilder => { - var hostingEnvironment = app.ApplicationServices.GetRequiredService(); - return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName); - })) + webBuilder + .ConfigureServices((context, services) => responseMessage = responseMessage ?? GetResponseMessage(context)) + .ConfigureKestrel(options => options + .Configure(options.ConfigurationLoader.Configuration) + .Endpoint("HTTP", endpointOptions => + { + if (responseMessage == null + && !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"])) + { + responseMessage = "Default Kestrel configuration not read."; + } + })) + .Configure(app => app.Run(context => + { + var hostingEnvironment = app.ApplicationServices.GetRequiredService(); + return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName); + })); + }) .Build().Run(); } diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs index 94c794aca845..db6face2bdcc 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs @@ -4,7 +4,6 @@ #pragma warning disable CS0618 // Type or member is obsolete using System; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HostFiltering; using Microsoft.AspNetCore.Hosting; @@ -21,31 +20,35 @@ static void Main(string[] args) { string responseMessage = null; - WebHost.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" }) - .ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context)) - .ConfigureKestrel(options => options - .Configure(options.ConfigurationLoader.Configuration) - .Endpoint("HTTP", endpointOptions => - { - if (responseMessage == null - && !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"])) - { - responseMessage = "Default Kestrel configuration not read."; - } - })) - .Configure(app => app.Run(context => + Host.CreateDefaultBuilder(new[] { "--cliKey", "cliValue" }) + .ConfigureWebHostDefaults(webBuilder => { - // Verify allowed hosts were loaded - var hostFilteringOptions = app.ApplicationServices.GetRequiredService>(); - var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts); - if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal)) - { - responseMessage = "AllowedHosts not loaded into Options."; - } + webBuilder + .ConfigureServices((context, service) => responseMessage = responseMessage ?? GetResponseMessage(context)) + .ConfigureKestrel(options => options + .Configure(options.ConfigurationLoader.Configuration) + .Endpoint("HTTP", endpointOptions => + { + if (responseMessage == null + && !string.Equals("KestrelEndPointSettingValue", endpointOptions.ConfigSection["KestrelEndPointSettingName"])) + { + responseMessage = "Default Kestrel configuration not read."; + } + })) + .Configure(app => app.Run(context => + { + // Verify allowed hosts were loaded + var hostFilteringOptions = app.ApplicationServices.GetRequiredService>(); + var hosts = string.Join(',', hostFilteringOptions.Value.AllowedHosts); + if (responseMessage == null && !string.Equals("example.com,127.0.0.1", hosts, StringComparison.Ordinal)) + { + responseMessage = "AllowedHosts not loaded into Options."; + } - var hostingEnvironment = app.ApplicationServices.GetRequiredService(); - return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName); - })) + var hostingEnvironment = app.ApplicationServices.GetRequiredService(); + return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName); + })); + }) .Build() .Run(); } diff --git a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs index 1cd83cc8fcb3..3cc9296bec30 100644 --- a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs +++ b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs @@ -4,11 +4,11 @@ #pragma warning disable CS0618 // Type or member is obsolete using System; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace CreateDefaultBuilderApp; @@ -16,27 +16,31 @@ public class Program { static void Main(string[] args) { - WebHost.CreateDefaultBuilder() - .UseUrls("http://127.0.0.1:0") - .ConfigureServices((context, services) => + Host.CreateDefaultBuilder() + .ConfigureWebHostDefaults(webBuilder => { - services.AddSingleton(typeof(IService<>), typeof(Service<>)); - services.AddScoped(); - }) - .Configure(app => - { - app.Run(context => - { - try + webBuilder + .UseUrls("http://127.0.0.1:0") + .ConfigureServices((context, services) => { - context.RequestServices.GetService>(); - return context.Response.WriteAsync("Success"); - } - catch (Exception ex) + services.AddSingleton(typeof(IService<>), typeof(Service<>)); + services.AddScoped(); + }) + .Configure(app => { - return context.Response.WriteAsync(ex.ToString()); - } - }); + app.Run(context => + { + try + { + context.RequestServices.GetService>(); + return context.Response.WriteAsync("Success"); + } + catch (Exception ex) + { + return context.Response.WriteAsync(ex.ToString()); + } + }); + }); }) .Build().Run(); } diff --git a/src/Security/Authentication/samples/SocialSample/Program.cs b/src/Security/Authentication/samples/SocialSample/Program.cs index bd5ef8aaef63..6ea08e2eb673 100644 --- a/src/Security/Authentication/samples/SocialSample/Program.cs +++ b/src/Security/Authentication/samples/SocialSample/Program.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.AspNetCore; +using Microsoft.Extensions.Hosting; namespace SocialSample; @@ -9,8 +9,11 @@ public static class Program { public static void Main(string[] args) { - var host = WebHost.CreateDefaultBuilder(args) - .UseStartup() + var host = Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }) .Build(); host.Run(); diff --git a/src/Security/samples/ClaimsTransformation/Program.cs b/src/Security/samples/ClaimsTransformation/Program.cs index baae17098bd5..0558a7a4fa93 100644 --- a/src/Security/samples/ClaimsTransformation/Program.cs +++ b/src/Security/samples/ClaimsTransformation/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace AuthSamples.ClaimsTransformer; @@ -17,12 +17,15 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args) + CreateHostBuilder(args) .Build() .Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/Cookies/Program.cs b/src/Security/samples/Cookies/Program.cs index 240ddd39b220..a9f09625f97c 100644 --- a/src/Security/samples/Cookies/Program.cs +++ b/src/Security/samples/Cookies/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace AuthSamples.Cookies; @@ -17,12 +17,15 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args) + CreateHostBuilder(args) .Build() .Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/CustomPolicyProvider/Program.cs b/src/Security/samples/CustomPolicyProvider/Program.cs index 10f33513900e..0065f0a3c09e 100644 --- a/src/Security/samples/CustomPolicyProvider/Program.cs +++ b/src/Security/samples/CustomPolicyProvider/Program.cs @@ -1,8 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Hosting; namespace CustomPolicyProvider; @@ -10,10 +10,13 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/DynamicSchemes/Program.cs b/src/Security/samples/DynamicSchemes/Program.cs index 00d316db6003..adc9fad08bec 100644 --- a/src/Security/samples/DynamicSchemes/Program.cs +++ b/src/Security/samples/DynamicSchemes/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace AuthSamples.DynamicSchemes; @@ -17,12 +17,15 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args) + CreateHostBuilder(args) .Build() .Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/Identity.ExternalClaims/Program.cs b/src/Security/samples/Identity.ExternalClaims/Program.cs index 76c69844b73e..29eecc68e7cf 100644 --- a/src/Security/samples/Identity.ExternalClaims/Program.cs +++ b/src/Security/samples/Identity.ExternalClaims/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace Identity.ExternalClaims; @@ -17,10 +17,13 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/PathSchemeSelection/Program.cs b/src/Security/samples/PathSchemeSelection/Program.cs index 03721b631e82..af44ce8957b2 100644 --- a/src/Security/samples/PathSchemeSelection/Program.cs +++ b/src/Security/samples/PathSchemeSelection/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace AuthSamples.PathSchemeSelection; @@ -17,10 +17,13 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } diff --git a/src/Security/samples/StaticFilesAuth/Program.cs b/src/Security/samples/StaticFilesAuth/Program.cs index 822a376c55d5..d0183d9aa6d6 100644 --- a/src/Security/samples/StaticFilesAuth/Program.cs +++ b/src/Security/samples/StaticFilesAuth/Program.cs @@ -6,9 +6,9 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; namespace StaticFilesAuth; @@ -17,10 +17,13 @@ public class Program { public static void Main(string[] args) { - CreateWebHostBuilder(args).Build().Run(); + CreateHostBuilder(args).Build().Run(); } - public static IWebHostBuilder CreateWebHostBuilder(string[] args) => - WebHost.CreateDefaultBuilder(args) - .UseStartup(); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); } From 410ec7923dfbcf545f6b8c430bd9ee0bf84ca0df Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 31 Jul 2025 20:38:17 +0000 Subject: [PATCH 5/7] Mark IWebHost interface as obsolete and clean up test assets Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- src/DefaultBuilder/src/ConfigureWebHostBuilder.cs | 2 ++ .../testassets/CreateDefaultBuilderApp/Program.cs | 2 -- .../testassets/CreateDefaultBuilderOfTApp/Program.cs | 2 -- .../testassets/DependencyInjectionApp/Program.cs | 2 -- .../src/HostingAbstractionsWebHostBuilderExtensions.cs | 2 ++ src/Hosting/Abstractions/src/IWebHost.cs | 1 + src/Hosting/Abstractions/src/IWebHostBuilder.cs | 2 ++ .../Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs | 2 ++ src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs | 2 ++ src/Hosting/Hosting/src/Internal/WebHost.cs | 2 ++ src/Hosting/Hosting/src/WebHostExtensions.cs | 2 ++ src/Hosting/Hosting/test/Fakes/GenericWebHost.cs | 2 ++ .../Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs | 2 ++ src/Hosting/Hosting/test/WebHostTests.cs | 4 ++++ src/Hosting/TestHost/src/TestServer.cs | 2 ++ src/Hosting/TestHost/src/WebHostBuilderExtensions.cs | 2 ++ src/Hosting/TestHost/test/ClientHandlerTests.cs | 2 ++ src/Hosting/TestHost/test/HttpContextBuilderTests.cs | 2 ++ src/Hosting/TestHost/test/TestClientTests.cs | 2 ++ src/Hosting/WindowsServices/src/WebHostService.cs | 2 ++ .../WindowsServices/src/WebHostWindowsServiceExtensions.cs | 2 ++ src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs | 2 ++ 22 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs index bd99297c8e56..8c00d7c26836 100644 --- a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs +++ b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Infrastructure; diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs index 36675cedf4d9..a3cbc83ab1c0 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs index db6face2bdcc..cd96ae23e1f9 100644 --- a/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs +++ b/src/DefaultBuilder/testassets/CreateDefaultBuilderOfTApp/Program.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.HostFiltering; diff --git a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs index 3cc9296bec30..6cd428fa86f8 100644 --- a/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs +++ b/src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs index 930fde23ac41..527fea5db635 100644 --- a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs +++ b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; diff --git a/src/Hosting/Abstractions/src/IWebHost.cs b/src/Hosting/Abstractions/src/IWebHost.cs index b770ff5cee3d..be026303020a 100644 --- a/src/Hosting/Abstractions/src/IWebHost.cs +++ b/src/Hosting/Abstractions/src/IWebHost.cs @@ -8,6 +8,7 @@ namespace Microsoft.AspNetCore.Hosting; /// /// Represents a configured web host. /// +[Obsolete("IWebHost is obsolete. Use IHost instead.")] public interface IWebHost : IDisposable { /// diff --git a/src/Hosting/Abstractions/src/IWebHostBuilder.cs b/src/Hosting/Abstractions/src/IWebHostBuilder.cs index dd3fc81fd207..26fbd3230d58 100644 --- a/src/Hosting/Abstractions/src/IWebHostBuilder.cs +++ b/src/Hosting/Abstractions/src/IWebHostBuilder.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; diff --git a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs index 85e384a1a2a7..29ca517a7e13 100644 --- a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Infrastructure; diff --git a/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs b/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs index c104ed40002a..0c80f74d4db1 100644 --- a/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs +++ b/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index 6626cdda9889..02507b6b8f0e 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; diff --git a/src/Hosting/Hosting/src/WebHostExtensions.cs b/src/Hosting/Hosting/src/WebHostExtensions.cs index 41c8e385d141..ae4e24c81f78 100644 --- a/src/Hosting/Hosting/src/WebHostExtensions.cs +++ b/src/Hosting/Hosting/src/WebHostExtensions.cs @@ -3,6 +3,8 @@ #nullable enable +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; diff --git a/src/Hosting/Hosting/test/Fakes/GenericWebHost.cs b/src/Hosting/Hosting/test/Fakes/GenericWebHost.cs index 1d6cd64871f4..2d3c97573d49 100644 --- a/src/Hosting/Hosting/test/Fakes/GenericWebHost.cs +++ b/src/Hosting/Hosting/test/Fakes/GenericWebHost.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http.Features; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs index 57f51e921138..73642e12d29c 100644 --- a/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs +++ b/src/Hosting/Hosting/test/Fakes/GenericWebHostBuilderWrapper.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Infrastructure; using Microsoft.Extensions.Configuration; diff --git a/src/Hosting/Hosting/test/WebHostTests.cs b/src/Hosting/Hosting/test/WebHostTests.cs index 36bb6a960e77..53358dc3662b 100644 --- a/src/Hosting/Hosting/test/WebHostTests.cs +++ b/src/Hosting/Hosting/test/WebHostTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Collections; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Fakes; @@ -1085,7 +1087,9 @@ public void ConfigureServices(IServiceCollection services, int gunk) { } public void Configure(IApplicationBuilder app) { } } +#pragma warning disable CS0618 // Type or member is obsolete private IWebHost CreateHost(RequestDelegate requestDelegate) +#pragma warning restore CS0618 // Type or member is obsolete { var builder = CreateBuilder() .UseFakeServer() diff --git a/src/Hosting/TestHost/src/TestServer.cs b/src/Hosting/TestHost/src/TestServer.cs index 1c9c295406ee..0e71380da88a 100644 --- a/src/Hosting/TestHost/src/TestServer.cs +++ b/src/Hosting/TestHost/src/TestServer.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Net.Http; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; diff --git a/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs b/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs index cbdb7e06e989..f49f86788510 100644 --- a/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs +++ b/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net.Http; diff --git a/src/Hosting/TestHost/test/ClientHandlerTests.cs b/src/Hosting/TestHost/test/ClientHandlerTests.cs index 170310ec275a..f34422804d4e 100644 --- a/src/Hosting/TestHost/test/ClientHandlerTests.cs +++ b/src/Hosting/TestHost/test/ClientHandlerTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Net; using System.Net.Http; using System.Text; diff --git a/src/Hosting/TestHost/test/HttpContextBuilderTests.cs b/src/Hosting/TestHost/test/HttpContextBuilderTests.cs index b46b2292339f..8ffb9fce44bc 100644 --- a/src/Hosting/TestHost/test/HttpContextBuilderTests.cs +++ b/src/Hosting/TestHost/test/HttpContextBuilderTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Text; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/src/Hosting/TestHost/test/TestClientTests.cs b/src/Hosting/TestHost/test/TestClientTests.cs index 2993c53e9aa6..05bf53d9524e 100644 --- a/src/Hosting/TestHost/test/TestClientTests.cs +++ b/src/Hosting/TestHost/test/TestClientTests.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Net; using System.Net.Http; using System.Net.WebSockets; diff --git a/src/Hosting/WindowsServices/src/WebHostService.cs b/src/Hosting/WindowsServices/src/WebHostService.cs index 89b9e72a2a4c..0b6925d4a7b5 100644 --- a/src/Hosting/WindowsServices/src/WebHostService.cs +++ b/src/Hosting/WindowsServices/src/WebHostService.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.ComponentModel; using System.ServiceProcess; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs b/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs index 950dd27b9f4a..0ec0a8a6bffe 100644 --- a/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs +++ b/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.ServiceProcess; namespace Microsoft.AspNetCore.Hosting.WindowsServices; diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index 926ab9bcf9b5..0c3666b9699c 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System.Linq; using System.Net.Http; using System.Reflection; From 2d43301307805af69e953474af4dd003a21445b9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 5 Aug 2025 19:45:19 +0000 Subject: [PATCH 6/7] Add pragma warning suppression for IWebHost obsolete warning in TransportTestHelpers Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- .../shared/test/TransportTestHelpers/IWebHostPortExtensions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Servers/Kestrel/shared/test/TransportTestHelpers/IWebHostPortExtensions.cs b/src/Servers/Kestrel/shared/test/TransportTestHelpers/IWebHostPortExtensions.cs index c2774908200c..19b8ed4387e1 100644 --- a/src/Servers/Kestrel/shared/test/TransportTestHelpers/IWebHostPortExtensions.cs +++ b/src/Servers/Kestrel/shared/test/TransportTestHelpers/IWebHostPortExtensions.cs @@ -1,6 +1,8 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +#pragma warning disable CS0618 // Type or member is obsolete + using System; using System.Collections.Generic; using System.Linq; From 8b4c6a9c78fc51378540b1c37005cb1764161cef Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 5 Aug 2025 23:32:50 +0000 Subject: [PATCH 7/7] Scope pragma warning disables to specific locations and mark classes as obsolete as requested Co-authored-by: BrennanConroy <7574801+BrennanConroy@users.noreply.github.com> --- src/DefaultBuilder/src/ConfigureWebHostBuilder.cs | 4 ++-- .../src/HostingAbstractionsWebHostBuilderExtensions.cs | 4 ++-- src/Hosting/Abstractions/src/IWebHostBuilder.cs | 4 ++-- .../Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs | 4 ++-- src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs | 4 ++-- src/Hosting/Hosting/src/Internal/WebHost.cs | 3 +-- src/Hosting/Hosting/src/WebHostExtensions.cs | 3 +-- src/Hosting/TestHost/src/TestServer.cs | 4 ++-- src/Hosting/TestHost/src/WebHostBuilderExtensions.cs | 4 ++-- src/Hosting/WindowsServices/src/WebHostService.cs | 3 +-- .../WindowsServices/src/WebHostWindowsServiceExtensions.cs | 3 +-- src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs | 4 ++-- 12 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs index 8c00d7c26836..e0fb8a86be07 100644 --- a/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs +++ b/src/DefaultBuilder/src/ConfigureWebHostBuilder.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Infrastructure; @@ -15,6 +13,7 @@ namespace Microsoft.AspNetCore.Builder; /// A non-buildable for . /// Use to build the . /// +#pragma warning disable CS0618 // Type or member is obsolete public sealed class ConfigureWebHostBuilder : IWebHostBuilder, ISupportsStartup { private readonly IWebHostEnvironment _environment; @@ -185,3 +184,4 @@ IWebHostBuilder ISupportsStartup.UseStartup([DynamicallyAccessedMembers(StartupL throw new NotSupportedException("UseStartup() is not supported by WebApplicationBuilder.WebHost. Use the WebApplication returned by WebApplicationBuilder.Build() instead."); } } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs index 527fea5db635..35578d72cebf 100644 --- a/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs +++ b/src/Hosting/Abstractions/src/HostingAbstractionsWebHostBuilderExtensions.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Linq; @@ -15,6 +13,7 @@ namespace Microsoft.AspNetCore.Hosting; /// /// Contains extension methods for configuring the . /// +#pragma warning disable CS0618 // Type or member is obsolete public static class HostingAbstractionsWebHostBuilderExtensions { /// @@ -178,3 +177,4 @@ public static IWebHost Start(this IWebHostBuilder hostBuilder, [StringSyntax(Str return host; } } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/Abstractions/src/IWebHostBuilder.cs b/src/Hosting/Abstractions/src/IWebHostBuilder.cs index 26fbd3230d58..efbbbf944828 100644 --- a/src/Hosting/Abstractions/src/IWebHostBuilder.cs +++ b/src/Hosting/Abstractions/src/IWebHostBuilder.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -12,6 +10,7 @@ namespace Microsoft.AspNetCore.Hosting; /// /// A builder for . /// +#pragma warning disable CS0618 // Type or member is obsolete public interface IWebHostBuilder { /// @@ -61,3 +60,4 @@ public interface IWebHostBuilder /// The . IWebHostBuilder UseSetting(string key, string? value); } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs index 29ca517a7e13..844804ca69c7 100644 --- a/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs +++ b/src/Hosting/Hosting/src/GenericHost/HostingStartupWebHostBuilder.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting.Infrastructure; @@ -14,6 +12,7 @@ namespace Microsoft.AspNetCore.Hosting; // We use this type to capture calls to the IWebHostBuilder so the we can properly order calls to // to GenericHostWebHostBuilder. +#pragma warning disable CS0618 // Type or member is obsolete internal sealed class HostingStartupWebHostBuilder : IWebHostBuilder, ISupportsStartup, ISupportsUseDefaultServiceProvider { private readonly GenericWebHostBuilder _builder; @@ -92,3 +91,4 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio return _builder.UseStartup(startupFactory); } } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs b/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs index 0c80f74d4db1..70dbd6d3dc74 100644 --- a/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs +++ b/src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs @@ -1,14 +1,13 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; namespace Microsoft.AspNetCore.Hosting; +#pragma warning disable CS0618 // Type or member is obsolete internal abstract class WebHostBuilderBase : IWebHostBuilder, ISupportsUseDefaultServiceProvider { private protected readonly IHostBuilder _builder; @@ -107,3 +106,4 @@ public IWebHostBuilder UseSetting(string key, string? value) return this; } } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/Hosting/src/Internal/WebHost.cs b/src/Hosting/Hosting/src/Internal/WebHost.cs index 02507b6b8f0e..e0b0857d982f 100644 --- a/src/Hosting/Hosting/src/Internal/WebHost.cs +++ b/src/Hosting/Hosting/src/Internal/WebHost.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; @@ -21,6 +19,7 @@ namespace Microsoft.AspNetCore.Hosting; +[Obsolete("WebHost is obsolete. Use Host.CreateDefaultBuilder or WebApplication.CreateBuilder instead.")] internal sealed partial class WebHost : IWebHost, IAsyncDisposable { private const string DeprecatedServerUrlsKey = "server.urls"; diff --git a/src/Hosting/Hosting/src/WebHostExtensions.cs b/src/Hosting/Hosting/src/WebHostExtensions.cs index ae4e24c81f78..d2912b1f7b42 100644 --- a/src/Hosting/Hosting/src/WebHostExtensions.cs +++ b/src/Hosting/Hosting/src/WebHostExtensions.cs @@ -3,8 +3,6 @@ #nullable enable -#pragma warning disable CS0618 // Type or member is obsolete - using Microsoft.AspNetCore.Hosting.Server.Features; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -14,6 +12,7 @@ namespace Microsoft.AspNetCore.Hosting; /// /// Contains extensions for managing the lifecycle of an . /// +[Obsolete("WebHostExtensions is obsolete. Use Host.CreateDefaultBuilder or WebApplication.CreateBuilder instead.")] public static class WebHostExtensions { /// diff --git a/src/Hosting/TestHost/src/TestServer.cs b/src/Hosting/TestHost/src/TestServer.cs index 0e71380da88a..de16709549bf 100644 --- a/src/Hosting/TestHost/src/TestServer.cs +++ b/src/Hosting/TestHost/src/TestServer.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Net.Http; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting.Server; @@ -18,6 +16,7 @@ namespace Microsoft.AspNetCore.TestHost; /// public class TestServer : IServer { +#pragma warning disable CS0618 // Type or member is obsolete private readonly IWebHost? _hostInstance; private bool _disposed; private ApplicationWrapper? _application; @@ -119,6 +118,7 @@ public IWebHost Host ?? throw new InvalidOperationException("The TestServer constructor was not called with a IWebHostBuilder so IWebHost is not available."); } } +#pragma warning restore CS0618 // Type or member is obsolete /// /// Gets the service provider associated with the test server. diff --git a/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs b/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs index f49f86788510..ba4bec0b11f2 100644 --- a/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs +++ b/src/Hosting/TestHost/src/WebHostBuilderExtensions.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Net.Http; @@ -16,6 +14,7 @@ namespace Microsoft.AspNetCore.TestHost; /// /// Contains extensions for configuring the instance. /// +#pragma warning disable CS0618 // Type or member is obsolete public static class WebHostBuilderExtensions { /// @@ -210,3 +209,4 @@ public Action ConfigureContainer(Action next) => }; } } +#pragma warning restore CS0618 // Type or member is obsolete diff --git a/src/Hosting/WindowsServices/src/WebHostService.cs b/src/Hosting/WindowsServices/src/WebHostService.cs index 0b6925d4a7b5..d850f8b68996 100644 --- a/src/Hosting/WindowsServices/src/WebHostService.cs +++ b/src/Hosting/WindowsServices/src/WebHostService.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.ComponentModel; using System.ServiceProcess; using Microsoft.Extensions.DependencyInjection; @@ -14,6 +12,7 @@ namespace Microsoft.AspNetCore.Hosting.WindowsServices; /// Provides an implementation of a Windows service that hosts ASP.NET Core. /// [DesignerCategory("Code")] +[Obsolete("Use UseWindowsService and AddHostedService instead.")] public class WebHostService : ServiceBase { private readonly IWebHost _host; diff --git a/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs b/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs index 0ec0a8a6bffe..345a7dfadbee 100644 --- a/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs +++ b/src/Hosting/WindowsServices/src/WebHostWindowsServiceExtensions.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.ServiceProcess; namespace Microsoft.AspNetCore.Hosting.WindowsServices; @@ -10,6 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.WindowsServices; /// /// Extensions to for hosting inside a Windows service. /// +[Obsolete("Use UseWindowsService and AddHostedService instead.")] public static class WebHostWindowsServiceExtensions { /// diff --git a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs index 0c3666b9699c..5e8d76efc5d1 100644 --- a/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs +++ b/src/Mvc/Mvc.Testing/src/WebApplicationFactory.cs @@ -1,8 +1,6 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#pragma warning disable CS0618 // Type or member is obsolete - using System.Linq; using System.Net.Http; using System.Reflection; @@ -25,6 +23,7 @@ namespace Microsoft.AspNetCore.Mvc.Testing; /// /// A type in the entry point assembly of the application. /// Typically the Startup or Program classes can be used. +#pragma warning disable CS0618 // Type or member is obsolete public partial class WebApplicationFactory : IDisposable, IAsyncDisposable where TEntryPoint : class { private bool _disposed; @@ -880,3 +879,4 @@ internal override WebApplicationFactory WithWebHostBuilderCore(Acti } } } +#pragma warning restore CS0618 // Type or member is obsolete