liveBook · Manning

Host.CreateDefaultBuilder()

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

Host.CreateDefaultBuilder() is a .NET factory method that creates an application host builder with default configuration, logging, environment, and dependency-injection settings.

Host.CreateDefaultBuilder() creates an IHostBuilder with default settings for configuration, logging, the hosting environment, and dependency injection (DI). You can customize the builder before calling Build() to create an IHost, then call Run() to start the application. section 30.7 section 30.2

The defaults include a content root, typically the current working directory, where the application can find configuration and Razor files. This is different from the web root, typically wwwroot, which holds static files. Host configuration uses DOTNET_-prefixed environment variables and command-line arguments to determine the hosting environment. section 30.4

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
            });
}

This Program.cs example passes command-line arguments to CreateDefaultBuilder, configures web hosting with Startup, and builds and runs the host. Startup configures application services and the middleware pipeline. section 30.2

The helper is similar in purpose to WebApplication.CreateBuilder(): both establish defaults. For many ASP.NET Core applications, minimal hosting with WebApplication is the simpler choice. The generic host can offer greater control when you need to customize the builder. section 30.4 section 30.0

Listing 30.6 Customizing the default HostBuilder
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }
 
    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging(logBuilder => logBuilder.AddSeq())  #1
            .ConfigureAppConfiguration((hostContext, config) =>   #2
  {
         config.Sources.Clear();                                  #3
config.AddJsonFile("appsettings.json");          #4
 
  }
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
#1 Adds the Seq logging provider to the configuration
#2 HostBuilder provides a hosting context and an instance of ConfigurationBuilder.
#3 Clears the providers configured by default in CreateDefaultBuilder
#4 Adds a JSON configuration provider, providing the filename of the configuration file

Customize the defaults by chaining configuration calls. This example adds the Seq logging provider, clears the default application configuration sources, and adds appsettings.json. Clearing sources replaces them rather than merely adding another source. section 30.4

You can also create a HostBuilder directly and configure it from scratch, but that means setting up behavior the helper would otherwise provide. Consider customizing the default builder first. section 30.4

The generic host is not limited to web applications. In .NET 7, a worker service can use CreateDefaultBuilder and ConfigureServices to register a background service and its dependencies before building and running the host. Non-ASP.NET Core applications can use the Microsoft.Extensions.Hosting package. section 34.2.1 section 30.4

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

How deep it goes
Explains selected defaults and customization, with a deliberately incomplete overview of the builder's construction.
Examples
Illustrates a Startup-based web host, a customized logging and configuration setup, and a worker service saving exchange rates.
What you can do after
Apply it via the generic host to build and run applications and customize builder configuration.
How it connects
Connects the generic host to dependency injection, configuration, logging, middleware, and minimal hosting.

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.