liveBook · Manning

Hosting model

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

A hosting model is the architecture and configuration that determines how an application is started, managed, and exposed by its host environment.

An ASP.NET Core application is a console application that runs a web server, typically Kestrel, inside its own process. Kestrel receives HTTP requests, passes them to the application, and returns responses. Unlike earlier ASP.NET applications, the application does not depend on IIS to call into it. section 27.1 section 2.4.2

There are two common ways to expose the application:

  • Expose Kestrel directly to the network. This is supported. section 2.4.2
  • Place a reverse proxy in front of it. The proxy receives internet requests and forwards them to the application. Examples include IIS on Windows and NGINX or Apache on Linux. section 27.1
Figure 27.1 The hosting model for ASP.NET Core gives flexibility. The same application can run exposed directly to the network, behind various reverse proxies without modification, and even inside the IIS process.
CH27_F01_Lock3

The figure compares direct exposure with hosting behind different reverse proxies. The same application can run in these arrangements without modification. section 27.1

To start an application using the Startup-based model, Main builds and runs an IHost. The host contains Kestrel, services, and configuration. Program sets up the host; Startup configures application services and the middleware pipeline. 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 Program.cs listing from CreateDefaultBuilder through Build and Run. UseStartup<Startup>() identifies where this arrangement places service and middleware configuration. section 30.2

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

How deep it goes
Explains the console-app process and request flow, then shows the build-and-run steps for Startup-based hosting.
Examples
Uses a hosting-arrangements figure and a Program.cs listing that builds and runs an IHost.
What it assumes
Introduces console-app entry points and reverse proxies while addressing readers familiar with .NET applications.
What you can do after
Operate/configure it by choosing direct or reverse-proxy exposure and following the Startup-based host initialization pattern.
How it connects
Connects Kestrel, reverse proxies, IIS, the generic host, dependency injection, and middleware.

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.