The CreateDefaultBuilder method adds a significant amount of convenience. Learn what that method does, and find out how to set the stage for customization of the process, which is used through the rest of this chapter.
- Another new feature in 2.0 is the CreateDefaultBuilder method off of the WebHost. And this encapsulates the most common tasks that you will do when you create a new project. It configures Kestrel and IISIntegration, it sets the content root directory, and it loads all the common configuration options, including the app settings, the appsettings.environmentname file, user secrets, if you're in development, environment variables, and the command-line arguments.
It also configures logging for console and debug using filtering rules from appsettings. Now that is new in the startup and we'll talk about that in the next section. You can add additional configuration logging providers and servers, and you can also execute Configure and ConfigureServices directly from the WebHostBuilder without having to use a startup class. So that last one of executing Configure, and ConfigureServices are brand new and they don't replace the startup class, they augment it.
So let's see how this works in code. When you create a new 2.0 asp.net project, the program class only has a few lines of code in it. The sample code that you're looking at has much more but that's because throughout the rest of this chapter I will show you different options. But the core code that comes with the default template is this method here. And it uses the CreateDefaultBuilder to set all those common options that seem to be used in just about every asp.net core project.
It then sets the startup class and build, and then sub-main will run it at the run. Now what is CreateDefaultBuilder doing? What I have done is I've added a method here called CreateDefaultBuilderExploded, and this shows exactly what is going on in the CreateDefaultBuilderMethod. Most of this should look very familiar to you. On line 74 it turns on Kestrel, it sets the content root. A new feature in 2.0 is that you can also configure the application right here and it sets the appsettings, the appsettings.environment file, if it's development it adds the user secrets down here on line 88, adds in the environment variables and the command line arguments.
This was all done in the constructor of the startup class in the templates for 1.0 and 1.1. Also new is configuring the logging right here, and it sets the console and debug loggers, and also adds in configuration, and we'll talk about that in the logging section, and then this should also be very familiar, UseIISIntegration, and the final thing is new as well in that here it is configuring the default dependency injection service provider, but this makes it very easy to plug in your own service provider for dependency injection.
You can also add in Configure and ConfigureServices and what I have in here is a class WebHostBuilder extension methods and this is just a theory of extension methods used throughout this course, and let's take a look at the used compression and cashing where we also call ConfigureServices and this is the same ConfigureServices that you would see in the startup as well as Configure.
You can add those all in the main method of the program class and keep your startup class very, very clean.
- Running and debugging ASP.NET Core applications
- Pros and cons of migrating existing applications to ASP.NET Core.
- Built-in dependency injection
- Environment awareness and app configuration
- Web host configuration and SSL
- View components invoked as tag helpers
- Configuration and logging
- Using Razor Pages