Skip to content

Replace WebHostBuilder with HostBuilder pattern in MVC folder #62703

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 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/Mvc/perf/benchmarkapps/BasicApi/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
Expand Down Expand Up @@ -229,25 +230,29 @@ private void DropDatabaseTables(IServiceProvider services)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
}
23 changes: 14 additions & 9 deletions src/Mvc/perf/benchmarkapps/BasicViews/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
Expand Down Expand Up @@ -191,26 +192,30 @@ private void DropDatabaseTables(IServiceProvider services)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseIISIntegration()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
}
21 changes: 13 additions & 8 deletions src/Mvc/perf/benchmarkapps/RazorRendering/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Html;
Expand Down Expand Up @@ -60,24 +61,28 @@ private static List<DataB> GenerateDataB()

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
public static IHostBuilder CreateHost(string[] args)
{
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

return new WebHostBuilder()
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
return new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseKestrel()
.UseUrls("http://+:5000")
.UseConfiguration(configuration)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>();
});
}
}
28 changes: 17 additions & 11 deletions src/Mvc/samples/MvcSandbox/Startup.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.

using Microsoft.Extensions.Hosting;

namespace MvcSandbox;

public class Startup
Expand Down Expand Up @@ -42,22 +44,26 @@ static void ConfigureEndpoints(IEndpointRouteBuilder endpoints)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging(factory =>
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
factory
.AddConsole()
.AddDebug();
})
.UseKestrel()
.UseStartup<Startup>();
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureLogging(factory =>
{
factory
.AddConsole()
.AddDebug();
})
.UseKestrel()
.UseStartup<Startup>();
});
}

19 changes: 12 additions & 7 deletions src/Mvc/test/WebSites/ApiExplorerWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using ApiExplorerWebSite.Controllers;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Hosting;

namespace ApiExplorerWebSite;

Expand Down Expand Up @@ -45,17 +46,21 @@ public void Configure(IApplicationBuilder app)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>();
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>();
});
}

20 changes: 13 additions & 7 deletions src/Mvc/test/WebSites/ApplicationModelWebSite/Startup.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.

using Microsoft.Extensions.Hosting;

namespace ApplicationModelWebSite;

public class Startup
Expand Down Expand Up @@ -34,17 +36,21 @@ public void Configure(IApplicationBuilder app)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
});
}

27 changes: 18 additions & 9 deletions src/Mvc/test/WebSites/BasicWebSite/Program.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;

namespace BasicWebSite;

public class Program
{
public static void Main(string[] args) => CreateWebHostBuilder(args).Build().Run();
public static void Main(string[] args)
{
using var host = CreateHost(args).Build();
host.Run();
}

// Do not change. This is the pattern our test infrastructure uses to initialize a IWebHostBuilder from
// a users app.
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
// This method now returns IHostBuilder and uses the new pattern with HostBuilder and ConfigureWebHost
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<StartupWithoutEndpointRouting>()
.UseKestrel()
.UseIISIntegration();
});
}

public class TestService
Expand Down
19 changes: 12 additions & 7 deletions src/Mvc/test/WebSites/ControllersFromServicesWebSite/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ControllersFromServicesWebSite.Components;
using ControllersFromServicesWebSite.TagHelpers;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.Extensions.Hosting;

namespace ControllersFromServicesWebSite;

Expand Down Expand Up @@ -61,17 +62,21 @@ public void Configure(IApplicationBuilder app)

public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
});
}

20 changes: 13 additions & 7 deletions src/Mvc/test/WebSites/CorsWebSite/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Hosting;

namespace CorsWebSite;

public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args)
using var host = CreateHost(args)
.Build();

host.Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
public static IHostBuilder CreateHost(string[] args) =>
new HostBuilder()
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseKestrel()
.UseIISIntegration();
});
}
Loading
Loading