ASP. NET Core application launch (translation)

Source: Internet
Author: User

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.

ConfigureServicesAndConfigure 方法在应用启动时由运行时调用。

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.