In this video, explore the application startup class to configure the services, the dependency injection container, and the application pipeline.
- [Narrator] 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 request. 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 to the http request, and then the configure services method configures any application services such as any DI framework context, custom code that you need, like repositories, and any other features as well as sets up to append the 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 used in Default Web Host Builder. So let's look at the startup class, and everything the startup class needs is injected into it by the framework. So starting in the constructor, it gets a hosting environment instance injected in, and that is used to get the environment.
So we see here. We're loading in 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 I configuration root instance. Let's go a little out of order of the file, but 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 a debug. Again, checking the environment, and if we are in development, we use a developer exception page which gives us much more information. It's a nag as to how the web config could be modified in web forms and NDC 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 Ace pedot 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 line 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 start up class, making the code much more concise and cleaner.
Let's go back to configure services where it gets the service collection 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 could run unhindered.
This is a convenience that I like to add. I add a singleton of the I configuration into the service container, the DI service container. So that if one of my classes downstream needs to get into configuration, I don't have to rebuild it. It can be injected in. This is actually much better in 2.0, and we'll 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 in prior versions. A lot of the plumbing is hooked up for us by the frame work, and it's just a cleaner start up.
- 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
- Web API improvements
- Support for creating GDPR-compliant applications