The Visual Studio template for creating an ASP.NET Core project, since version 3.x, adds the following (or very similar) contents to the Program class:
This has changed a bit since previous versions and is now more opinionated; I already showed this when talking about OWIN earlier in this chapter.
The Host class exposes the static CreateDefaultBuilder, which returns a fully built IHostBuilder instance. The CreateDefaultBuilder method is actually doing a lot of things behind our backs:
Creates a ConfigurationBuilder and adds the environment variables provider to it (see Chapter 2, Configuration, for more details)
Adds the appsettings.json (mandatory) and appsettings.<environment>.json (optional) JSON files and provider to the configuration builder
Configures the user secrets configuration, if running in development mode
Configures command-line configuration, if command-line arguments were passed
Sets Kestrel as the host to use and loads Kestrel-related configurations
Sets the content root to be the current directory
Sets the host to use the URLs passed as the ASPNETCORE_SERVER.URLSenvironment variable, if it exists
Configures logging to the console, debug, EventSource, and EventLog (if in Windows)
Adds IIS integration
Sets the default host lifetime as ConsoleHostLifetime
Configures service provider parameters to validate the scope of registered services and lifetimes if running in the Development environment
Registers some services, such as IConfiguration
These are the defaults you get, but you can override any of them by using some extension methods over the IHostBuilder interface:
Host .CreateDefaultBuilder(args) .ConfigureAppConfiguration((context, builder) => { //add or remove from the configuration builder }) .ConfigureContainer<MyContainer>((context, container) => { //configure container }) .ConfigureLogging((context, builder) => { //add or remove from the logging builder }); .ConfigureServices(services => { //register services }) .ConfigureWebHostDefaults(builder => { builder.UseStartup<Startup>(); });
After the default builder is instantiated, we ask it to use the Startup class, which is where we can configure the exact stuff we want, such as registered services, middleware components, and so on
IHostBuilder then builds an IHost and then we ask it to run. This is what actually gets our application working.
We have talked about the Startup class before. Basically, it exposes two methods, named ConfigureServices and Configureby convention; the first is used to register services and their implementations with the default DI provider (and possibly use a different one), and the second one is used to add middleware components to the ASP.NET Core pipeline.
The main things you need to remember here are as follows:
Kestrel is the default host server.
Configuration providers for JSON and the environment are added automatically; user secrets are added if running in Development environment. There should be one appsettings.json file and possibly one appsettings.<environment>.json file, with overrides per environment.
Logging is enabled for the console and debug pane of Visual Studio.
Now that we have looked at these templates, let's see what has changed since version 2.0 and how the different tools, templates, features, and so on are affected by it.