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 7 commits into
base: main
Choose a base branch
from
16 changes: 10 additions & 6 deletions src/Components/test/testassets/ComponentsApp.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -12,10 +13,13 @@ public static void Main(string[] args)
BuildWebHost(args).Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});

public static IWebHost BuildWebHost(string[] args) =>
CreateWebHostBuilder(args).Build();
public static IHost BuildWebHost(string[] args) =>
CreateHostBuilder(args).Build();
}
11 changes: 8 additions & 3 deletions src/DefaultBuilder/samples/SampleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -73,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<Startup>()
// Using defaults with HostBuilder pattern
using (var host = Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.Build())
{
host.Run();
Expand Down
2 changes: 2 additions & 0 deletions src/DefaultBuilder/src/ConfigureWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Builder;
/// A non-buildable <see cref="IWebHostBuilder"/> for <see cref="WebApplicationBuilder"/>.
/// Use <see cref="WebApplicationBuilder.Build"/> to build the <see cref="WebApplicationBuilder"/>.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public sealed class ConfigureWebHostBuilder : IWebHostBuilder, ISupportsStartup
{
private readonly IWebHostEnvironment _environment;
Expand Down Expand Up @@ -183,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
2 changes: 2 additions & 0 deletions src/DefaultBuilder/src/GenericHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/DefaultBuilder/src/WebApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Microsoft.AspNetCore;
/// <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.")]
public static class WebHost
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
38 changes: 21 additions & 17 deletions src/DefaultBuilder/testassets/CreateDefaultBuilderApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// The .NET Foundation licenses this file to you under the MIT license.

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;

Expand All @@ -16,23 +16,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<IWebHostEnvironment>();
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<IWebHostEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}));
})
.Build().Run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.HostFiltering;
using Microsoft.AspNetCore.Hosting;
Expand All @@ -19,31 +18,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<IOptions<HostFilteringOptions>>();
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<IOptions<HostFilteringOptions>>();
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<IHostEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}))
var hostingEnvironment = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
return context.Response.WriteAsync(responseMessage ?? hostingEnvironment.ApplicationName);
}));
})
.Build()
.Run();
}
Expand Down
44 changes: 24 additions & 20 deletions src/DefaultBuilder/testassets/DependencyInjectionApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
// 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.

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;

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<IAnotherService, AnotherService>();
})
.Configure(app =>
{
app.Run(context =>
{
try
webBuilder
.UseUrls("http://127.0.0.1:0")
.ConfigureServices((context, services) =>
{
context.RequestServices.GetService<IService<IAnotherService>>();
return context.Response.WriteAsync("Success");
}
catch (Exception ex)
services.AddSingleton(typeof(IService<>), typeof(Service<>));
services.AddScoped<IAnotherService, AnotherService>();
})
.Configure(app =>
{
return context.Response.WriteAsync(ex.ToString());
}
});
app.Run(context =>
{
try
{
context.RequestServices.GetService<IService<IAnotherService>>();
return context.Response.WriteAsync("Success");
}
catch (Exception ex)
{
return context.Response.WriteAsync(ex.ToString());
}
});
});
})
.Build().Run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Hosting;
/// <summary>
/// Contains extension methods for configuring the <see cref="IWebHostBuilder" />.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public static class HostingAbstractionsWebHostBuilderExtensions
{
/// <summary>
Expand Down Expand Up @@ -176,3 +177,4 @@ public static IWebHost Start(this IWebHostBuilder hostBuilder, [StringSyntax(Str
return host;
}
}
#pragma warning restore CS0618 // Type or member is obsolete
1 change: 1 addition & 0 deletions src/Hosting/Abstractions/src/IWebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Microsoft.AspNetCore.Hosting;
/// <summary>
/// Represents a configured web host.
/// </summary>
[Obsolete("IWebHost is obsolete. Use IHost instead.")]
public interface IWebHost : IDisposable
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Hosting/Abstractions/src/IWebHostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Microsoft.AspNetCore.Hosting;
/// <summary>
/// A builder for <see cref="IWebHost"/>.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
public interface IWebHostBuilder
{
/// <summary>
Expand Down Expand Up @@ -59,3 +60,4 @@ public interface IWebHostBuilder
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
IWebHostBuilder UseSetting(string key, string? value);
}
#pragma warning restore CS0618 // Type or member is obsolete
Original file line number Diff line number Diff line change
Expand Up @@ -12,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;
Expand Down Expand Up @@ -90,3 +91,4 @@ public IWebHostBuilder UseStartup([DynamicallyAccessedMembers(StartupLinkerOptio
return _builder.UseStartup(startupFactory);
}
}
#pragma warning restore CS0618 // Type or member is obsolete
2 changes: 2 additions & 0 deletions src/Hosting/Hosting/src/GenericHost/WebHostBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Microsoft.AspNetCore.Hosting;

#pragma warning disable CS0618 // Type or member is obsolete
internal abstract class WebHostBuilderBase : IWebHostBuilder, ISupportsUseDefaultServiceProvider
{
private protected readonly IHostBuilder _builder;
Expand Down Expand Up @@ -105,3 +106,4 @@ public IWebHostBuilder UseSetting(string key, string? value)
return this;
}
}
#pragma warning restore CS0618 // Type or member is obsolete
1 change: 1 addition & 0 deletions src/Hosting/Hosting/src/Internal/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,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";
Expand Down
Loading