ASP. NET Core Chinese Document Chapter 3 principle (1) application startup, asp. netcore

Source: Internet
Author: User

ASP. NET Core Chinese Document Chapter 3 principle (1) application startup, asp. netcore

Original article: Application Startup
Author: Steve Smith
Translation: Liu Yi (AlexLEWIS)
Proofreading: Xie Yi (kiler398) and Xu dengyang (Seay)

ASP. NET Core provides complete control for your application to process each request.StartupA class is the entry point of an application. This class can be configured to connect the services to be used by the application. Developers canStartupClass, which is used to process all requests of the application.

Chapter:

  • Startup class
  • Configure Method
  • ConfigureServices Method
  • Service available at startup
  • Additional reading
Startup class

In ASP. NET Core,StartupClass provides the entry to the application, and all applications haveStartupClass. There may be startup classes and methods in a specific environment (see Working with Multiple Environments). However,StartupClass will be used as the startup point of the application. ASP. NET searchesStartupIn any namespace ). You can specify another assembly for retrieval.Hosting: ApplicationConfiguration key. ASP. NET is not concernedStartupIs the class definedpublicIf it complies with the naming rules, ASP. NET will continue to load it. If there are multipleStartupAnd no exception is triggered. ASP. NET selects one of them based on the namespace (matching the root namespace of the project takes priority; otherwise, the class in the first namespace in alphabetical order is used ).

Configure Method

ConfigureSpecifies how ASP. NET Applications respond to each HTTP request. Simply put, you can configure each request to receive the same response. However, most real-world applications require much more functionality than this. More complex MPs queue configurations can be encapsulated in middleware (middleware) and added to IApplicationBuilder through extension.

ConfigureThe method must accept an IApplicationBuilder parameter. Some additional services, suchIHostingEnvironmentOrILoggerFactoryIt can also be specified. If they are available, these services will be injected into the server. In the following example (derived from the default Web site template), multiple extension methods are used to configure pipelines to support BrowserLink, error pages, static files, ASP. net mvc, and Identity.

Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {loggerFactory. addConsole (Configuration. getSection ("Logging"); loggerFactory. addDebug (); if (env. isDevelopment () {app. useDeveloperExceptionPage (); // manually highlight the app. useDatabaseErrorPage (); // manually highlight the app. useBrowserLink (); // manually highlight} else {app. useExceptionHandler ("/Home/Error"); // manually highlight} app. useStaticFiles (); // hand Highlighting app. UseIdentity (); // manually highlighting // Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink? LinkID = 532715 app. useMvc (routes => // manually highlight {routes. mapRoute (name: "default", template: "{controller = Home}/{action = Index}/{id ?} ");});}

EachUseThe Extension Method adds a middleware to the request pipeline. For exampleUseMvcThe Extension Method adds the routing middleware to the request pipeline and configures MVC as the default processor.

In the Middleware chapter, you can learn more about Middleware and use IApplicationBuilder to define request pipelines.

ConfigureServices Method

YourStartupClass can include an optionalConfigureServicesMethod is used to configure the services used in the application.ConfigureServicesThe method isStartupClass, get an IServiceCollection instance through the parameter and return optionalIServiceProvider.ConfigureServicesYou mustConfigurePreviously called. This is very important because some functions in ASP. net mvc needConfigureServicesAnd these services must be added before the access request pipeline.ConfigureServices.

As throughConfigure, We recommend that you use the extension method on IServiceCollection to wrapConfigureServices. You can see severalAdd[Something]The extension method is used to set applications so that Entity Framework, Identity, and MVC can be used:

Public void ConfigureServices (IServiceCollection services) {// Add framework services. services. addDbContext <ApplicationDbContext> (options => // manually highlight options. useSqlServer (Configuration. getConnectionString ("DefaultConnection"); services. addIdentity <ApplicationUser, IdentityRole> () // manually highlight. addEntityFrameworkStores <ApplicationDbContext> (). addDefaultTokenProviders (); services. addMvc (); // manually highlight // Add application services. services. addTransient <IEmailSender, AuthMessageSender> (); services. addTransient <ISmsSender, AuthMessageSender> ();}

Dependency injection allows you to add services to a service container to make them available in applications. AsStartupClass can use the specified dependency as its method parameter -- rather than hard-coding) to instantiate a specific implementation-this can be done for middleware, MVC controllers, and other classes in the application.

ConfigureServicesThe method can also be used to add a configuration option class (in the preceding exampleAppSettings), As long as you want it to take effect in the application. For more information about Configuration options, see Configuration.

Service available at startup

ASP. NET Core provides some application services and objects during application startup. You can use these services very simply.StartupClass constructor or itsConfigureAndConfigureServicesThe method contains an appropriate interface. The following definesStartupServices available for each method in the class. Framework Services and objects include:

IApplicationBuilder
The request pipeline used to build the application. You can onlyStartupInConfigureMethod. For more information, see Features.

IApplicationEnvironment
Provides access to application properties, similarApplicationName,ApplicationVersionAndApplicationBasePath. You canStartupConstructor andConfigureMethod.

IHostingEnvironment
Provides the currentEnvironmentName,WebRootPathAnd the Web root file provider. You canStartupConstructor andConfigureMethod.

ILoggerFactory
Provides a log creation mechanism. You canStartupConstructor orConfigureMethod. For more information, see Logging.

IServiceCollection
The configuration set of each service in the current container. OnlyConfigureServicesMethod. You can configure the method to make the service available in the application.

LookStartupEach method in the class ordered by their calls. The following service can be used as a parameter:
Startup Constructor-IApplicationEnvironment-IHostingEnvironment-ILoggerFactory

ConfigureServices-IServiceCollection

Configure-IApplicationBuilder-IApplicationEnvironment-IHostingEnvironment-ILoggerFactory

Note:
AlthoughILoggerFactoryIt is available in constructors, but it is usuallyConfigureMethod. For more information, see Logging.

Additional reading
  • Work in multiple Environments
  • Middleware
  • . NET open Web interface (OWIN)

Returned directory

Reference page:

Http://www.yuanjiaocheng.net/ASPNET-CORE/setup-mvc.html

Http://www.yuanjiaocheng.net/entity/setenvrionment.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-stream.html

Http://www.yuanjiaocheng.net/mvc/mvc-helper-DropDownList.html

Http://www.yuanjiaocheng.net/webapi/test-webapi.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-collection.html

Http://www.yuanjiaocheng.net/Linq/linq-tutorials.html

Http://www.yuanjiaocheng.net/ASPNET-CORE/core-authorize-attribute.html

Http://www.yuanjiaocheng.net/Linq/linq-lambda-expression.html

Http://www.yuanjiaocheng.net/Linq/linq-api.html

Http://www.yuanjiaocheng.net/CSharp/Csharp-throw.html

Related Article

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: info-contact@alibabacloud.com 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.