- ASP.NET Core apps configure and launch a host.
- The host is responsible for app startup and lifetime management.
- At a minimum, the host configures a server and a request processing pipeline.
- Create a host using an instance of WebHostBuilder. This is typically performed in the app's entry point, the
Main
method.
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
CreateDefaultBuilder
performs the following tasks:
- Configures Kestrel as the web server.
- Sets the content root to the path returned by Directory.GetCurrentDirectory.
- Loads optional configuration from:
- appsettings.json.
- appsettings.{Environment}.json.
- User secrets when the app runs in the
Development
environment.
- Environment variables.
- Command-line arguments.
- Configures logging for console and debug output.
- Logging includes log filtering rules specified in a Logging configuration section of an appsettings.json or appsettings.{Environment}.json file.
- When running behind IIS, enables IIS integration.
- Configures the base path and port the server listens on when using the ASP.NET Core Module.
- The module creates a reverse proxy between IIS and Kestrel.
- Also configures the app to capture startup errors.
- Sets ServiceProviderOptions.ValidateScopes to
true
if the app's environment is Development.
- The content root determines where the host searches for content files, such as MVC view files.
- When the app is started from the project's root folder, the project's root folder is used as the content root.
- This is the default used in Visual Studio and the dotnet new templates.
- An ASP.NET Core app runs with an in-process HTTP server implementation.
- The server implementation listens for HTTP requests and surfaces them to the app as sets of request features composed into an HttpContext.
- ASP.NET Core ships two server implementations:
Kestrel
- Kestrel is the web server that's included by default in ASP.NET Core new-project templates.
- Kestrel can be used by itself or with a reverse proxy server, such as IIS, Nginx, or Apache.