liveBook · Manning

CreateHostBuilder

As explained in ASP.NET Core in Action, 3e

CreateHostBuilder is a helper method that constructs and configures the generic host builder for an application.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the helper’s call chain and responsibilities, but defers detailed service registration and middleware configuration to Startup.
Examples
Uses a Program.cs listing that calls CreateHostBuilder, Build, Run, ConfigureWebHostDefaults, and UseStartup.
What it assumes
Assumes basic C# application structure while introducing IHostBuilder, IHost, and the Program-to-Startup configuration split.
What you can do after
Operate/configure an ASP.NET Core application using the conventional generic-host startup pattern.
How it connects
Connects the generic host with IHostBuilder, Startup, dependency injection, middleware pipeline, and web-host defaults.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.