This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18686|Reply: 0

[Communication] Startup class is explained in detail

[Copy link]
Posted on 2/11/2019 7:48:46 PM | | | |
ASP.NET Analysis of Core Operating Principles: Startup
Startup Class
1. Startup Constructor
2、ConfigureServices
3. Configure method
Startup Class

The Startup Class contains two important methods: the Configure method is used to handle each HTTP request, such as the Middleware mentioned later, which is configured in the configure method. The ConfigureServices method is called before the Configure method, which is an optional method that can be used in the configureServices dependency injection interface or some global framework, such as EntityFramework, MVC, etc.Startup class's execution order: construct -> configureServices->configure

1. Startup Constructor

The main implementation of some configurations is as follows:

  • IHostingEnvironment:Special properties for accessing the application, such as:applicationName,applicationVersion。 PassIHostingEnvironmentProperties under objects can be configured in the construct. For example, get the current root path, find the configuration json file address, then initialize the configuration file in ConfigurationBuilder, and finally get the configuration file through the GetSection() method. The list of codes is as follows:The configuration file under the root directory is as follows:ILoggerFactory: 提供创建日志的接口,可以选用已经实现接口的类或自行实现此接口,下面代码使用最简单的控制台作为日志输出。
  • 2. ConfigureServices mainly implements the configuration of dependency injection (DI), and the method parameters are as follows:
    IServiceCollection: The entire ASP.NET Core comes with dependency injection (DI) by default, IServiceCollection is the container for dependency injection, first create a class (Foo) and interface (IFoo), the code list is as follows:Inject interfaces and implementations into containers in ConfigureServicesIf you want to use the IFoo's GetFoo() method to handle it after every Http request, you can register the function in the Configure method, and since the registration process uses dependency injection (DI), you can directly take the <IFoo>IFoo object out of the container through the RequestServices.GetRequiredService() generic method.In addition to its own interfaces, it also supports adding more injection methods through extension methods, such as EntityFramework and mvc frameworks.3. Configure method

    Mainly HTTP processing pipeline configuration and some system configuration, the parameters are as follows:

    • IApplicationBuilder:Used to build app request pipelines. Pass in the pipeline processing method through the run method under IApplicationBuilder. This is the most common method, and for applications in a real environment, it basically requires permission verification, cross-domain, exception handling, etc. The following code calls the IApplicationBuilder.Run method to register the handler. Intercept every HTTP request and output Hello World.IHostingEnvironment: 同构造参数ILoggerFactory: 同构造参数






Previous:Chinese version of 3ds Max renderings to make classroom records
Next:Grokking Deep Learning
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com