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.Startup
A class is the entry point of an application. This class can be configured to connect the services to be used by the application. Developers canStartup
Class, 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,Startup
Class provides the entry to the application, and all applications haveStartup
Class. There may be startup classes and methods in a specific environment (see Working with Multiple Environments). However,Startup
Class will be used as the startup point of the application. ASP. NET searchesStartup
In any namespace ). You can specify another assembly for retrieval.Hosting: ApplicationConfiguration key. ASP. NET is not concernedStartup
Is the class definedpublic
If it complies with the naming rules, ASP. NET will continue to load it. If there are multipleStartup
And 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
Configure
Specifies 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.
Configure
The method must accept an IApplicationBuilder parameter. Some additional services, suchIHostingEnvironment
OrILoggerFactory
It 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 ?} ");});}
EachUse
The Extension Method adds a middleware to the request pipeline. For exampleUseMvc
The 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
YourStartup
Class can include an optionalConfigureServices
Method is used to configure the services used in the application.ConfigureServices
The method isStartup
Class, get an IServiceCollection instance through the parameter and return optionalIServiceProvider
.ConfigureServices
You mustConfigure
Previously called. This is very important because some functions in ASP. net mvc needConfigureServices
And 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. AsStartup
Class 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.
ConfigureServices
The 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.Startup
Class constructor or itsConfigure
AndConfigureServices
The method contains an appropriate interface. The following definesStartup
Services available for each method in the class. Framework Services and objects include:
IApplicationBuilder
The request pipeline used to build the application. You can onlyStartup
InConfigure
Method. For more information, see Features.
IApplicationEnvironment
Provides access to application properties, similarApplicationName
,ApplicationVersion
AndApplicationBasePath
. You canStartup
Constructor andConfigure
Method.
IHostingEnvironment
Provides the currentEnvironmentName
,WebRootPath
And the Web root file provider. You canStartup
Constructor andConfigure
Method.
ILoggerFactory
Provides a log creation mechanism. You canStartup
Constructor orConfigure
Method. For more information, see Logging.
IServiceCollection
The configuration set of each service in the current container. OnlyConfigureServices
Method. You can configure the method to make the service available in the application.
LookStartup
Each 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:
AlthoughILoggerFactory
It is available in constructors, but it is usuallyConfigure
Method. 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