Startup is used to configure the request pipeline for services and apps.
The Startup class
The ASP. NET Core app uses the startup class. Startup class:
1. Optionally include the Configureservices method to configure the service of the application.
2. You must include a configure method to create the application's request processing pipeline.
ConfigureServices
AndConfigure 方法在应用启动时由运行时调用。
1 Public classStartup2 {3 //Use this method to add services to the container.4 Public voidconfigureservices (iservicecollection services)5 {6 ...7 }8 9 //Use this method to configure the HTTP request pipeline.Ten Public voidConfigure (Iapplicationbuilder app) One { A ... - } -}
The Webhostbuilderextensions usestartup<tstartup> method Startup
is clear, as follows:
Public class program{ publicstaticvoid Main (string[] args) { Buildwebhost (args). Run (); } Public Static Iwebhost buildwebhost (string[] args) = webhost.createdefaultbuilder (args) . Usestartup<Startup>() . Build ();}
The Startup class constructor accepts a dependency defined by the host. A common use of the dependency injection startup class is to inject ihostingenvironment through the Environment Configuration service:
Public classstartup{ PublicStartup (ihostingenvironment env) {hostingenvironment=env; } PublicIhostingenvironment hostingenvironment {Get; } Public voidconfigureservices (iservicecollection services) {if(Hostingenvironment.isdevelopment ()) {//Development Configuration } Else { //staging/production Configuration } }}
An alternative way to inject ihostingstartup is by using conventions. The application defines a separate startup class for different environments (for example: Startupdevelopment), and the runtime chooses the appropriate startup class. A class with a name suffix that matches the current environment is preferred . If the app is running in the development environment, it also contains Startup
classes andStartupDevelopment 类,StartupDevelopment 类将会被使用。
The Configureservices method
Configureservices Method:
1. Optional
2. Services configured by the web host to configure the application before the Configure method
3. Configuration options are set by convention
Translated into official documents. If you have not done so, you are welcome to correct me.
ASP. NET Core application launch (translation)