liveBook · Manning

ConfigureWebHostDefaults

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

ConfigureWebHostDefaults is a host-builder method that applies default configuration for an ASP.NET Core web host, including configuring Kestrel.

ConfigureWebHostDefaults adds web-host defaults to an IHostBuilder. It configures the WebHostBuilder, including Kestrel, so an ASP.NET Core application can listen for HTTP requests. section 30.2

The method complements Host.CreateDefaultBuilder rather than replacing it. The generic host supplies shared features such as configuration, logging, and dependency injection; web-host defaults add the services needed for HTTP applications. section 30.2

Configure a Startup-based web host

CreateHostBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); The resulting host can be built and run with Build().Run(). Use Startup to register dependency-injection services and configure the middleware pipeline. section 30.2 section 30.7

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

See the complete Program.cs flow from creating the host builder through building and running the IHost. section 30.2

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

How deep it goes
Explains its role, Kestrel configuration, generic-host relationship, and Startup callback, but defers custom host construction.
Examples
Uses a Program.cs example that calls Host.CreateDefaultBuilder, ConfigureWebHostDefaults, UseStartup, Build, and Run.
What it assumes
Assumes familiarity with C# lambdas and host-building concepts while distinguishing generic-host configuration from web-host configuration.
What you can do after
Build and run a Startup-based ASP.NET Core host that configures services and its HTTP middleware pipeline.
How it connects
Connects generic host, IHostBuilder, WebHostBuilder, Kestrel, Startup, dependency injection, middleware, and configuration.

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.