This video covers the application startup class to configure the services, the dependency injection container, and the application pipeline.
- [Instructor] The application startup file was specified in the web host builder, and this is where we configure services and also how the application will accept and respond to HTTP requests. The constructor in 1.0 and 1.1 creates a configuration builder, user secrets, pretty much anything having to do with configuration of the application. The configure method deals with specifically how the application responds the HTTP requests, and then the configure services method configures any application services such as any DI framework contexts, custom code that you need like repositories, and any other features, as well as sets up the dependency injection container.
Now there is a fairly significant change to this in 2.0, which we'll certainly cover in that chapter, but it takes the configuration code out of this class and moves it into the void main method using DefaultWebHostBuilder. So let's look at the Startup class, and everything the Startup class needs in injected into it by the framework. So starting in the constructor it gets the hosting environment instance injected in, and that is used to get the environment.
So we see here, we're loading the appropriate files based on the environment. We've already covered a lot of this material. If we're in development then we add user secrets, add any environment variables, and then we assign the result of the configuration builder to this configuration property, which is an IConfigurationRoot instance. Let's go a little out of order of the file, go down to Configure, and the Configure gets the application builder, the hosting environment, and a logger factory injected in.
We'll cover logging shortly, but this is where we add a console and the debug, again, checking the environment, and if we are in development, we'll use a developer exception page, which gives us much more information. It's analogous to how the web config could be modified in web forms and MVC to show more error code. We have browser link, and then here is some custom code I wrote to initialize the data for the application.
Otherwise, we're running in production. Well, we're running in not development to be completely accurate, and we're going to use the standard exception handler. Everything in ASP.NET Core is opt in. So if you wanted to use static files, you would have to specify to allow using static files. Line 101 adds an identity, and then like 105 is where we create the route table for MVC. So a lot of the MVC 5 files, the off config, the route config, all of those have been replaced by this single startup class making the code much more concise and cleaner.
Now let's go back to ConfigureServices where it gets the ServiceCollection injected in, and we have some code that was created for us, but we'll go ahead and look at it. On line 54 through 56 we are adding in to the service container the DB context, we're adding in identity, and then we add MVC, and here are the different versions. I covered SSL, and as promised I commented those all out so you can run uninhindered.
This is a convenience that I like to add. I add a singleton of the IConfigurationRoot into the service container, the DI service container, so that if one of my classes downstream needs to get into the configuration, I don't have to rebuild it, it can be injected in. This is actually much better in 2.0. It will cover that in that chapter, but this is a great trick for 1.0 and 1.1. Here are my custom repos added in to the dependency injection container, and then the custom settings that we've already talked about.
So we have one file that takes the place of multiple files and prior versions. Lot of the plumbing is hooked up for us by the framework, and it's just a cleaner startup.
- 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