CreateHostBuilder is a helper method that builds and configures an IHostBuilder for an ASP.NET Core application. The usual pattern calls Host.CreateDefaultBuilder(args), applies web-host defaults, selects Startup, then builds and runs the IHost. This keeps stable application bootstrapping in Program while feature-specific services and middleware remain in Startup. section 30.7 section 30.2
Listing 30.1 The Program.cs file configures and runs an IHost
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args) #1
.Build() #2
.Run(); #3
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args) #4
.ConfigureWebHostDefaults(webBuilder => #5
{
webBuilder.UseStartup<Startup>(); #6
});
}
Follow the complete Program.cs flow: create the builder, configure web-host defaults, select Startup, build the host, and run the application. section 30.2
The standard method performs these steps:
- Accept command-line arguments for host configuration. section 30.7
- Load default logging, configuration providers, environment settings, and dependency-injection configuration through Host.CreateDefaultBuilder. section 30.7
- Configure web-host defaults and delegate service registration and middleware setup to Startup. section 30.7
