Startup processing in different environments and startup Processing
ASP. NET Core introduces further support for controlling application behavior in multiple environments, such as Development Environment, Staging Environment, and Production Environment ). The ASPNETCORE_ENVIRONMENT is used to indicate the environment in which the program runs and allow the program to be correctly configured.
I. Runtime Environment
Development Environment: used when developing a program.
Pre-release environment (Staging): By convention, the Staging environment is a pre-production environment Used for final testing before being released to the product environment.
Production Environment: an environment that is running and used by end users. This environment should be configured to maximize security, efficiency, and robustness.
II. Environment Variable (ASPNETCORE_ENVIRONMENT)
ASP. NET Core uses a special environment variable called ASPNETCORE_ENVIRONMEN to describe the environment in which the application is currently running. This variable can be set to any of your favorite values, but it is customary to use these three values: Development, Staging, and Production. You will find these values in the samples and templates provided by ASP. NET Core.
In Windows and macOS, environment variable names are case-insensitive. Whether you set it to Development, development, or DEVELOPMENT, the results are the same. However, by default, Linux is case sensitive. For best practices, environment variables, file names, and settings should be assumed case sensitive.
3. Set Environment Variables
For how to set environment variables in different environments, refer to Microsoft's official documentation: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/environments#setting-the-environment
During development, we can directly modify the environment variables (as shown in) on the settings page of Visual Studio without modifying the settings in the system.
The settings on this property page are persistently stored in the launchSettings. json file. This file is located under the Properties folder of the project.
4. Load different Startup classes according to different environments
In the previous article, the main method of the program uses UseStartup <TStartup>. This method has multiple reloads. In addition to loading the specified Startup class, you can also search for a specific Startup class in the specified program set based on the current running environment. The search rule is "Startup {EnvironmentName }". For example, if the current program runs in the development environment, this method will find the class named "StartupDevelopment" in the specified program.
ASP. NET Core also supports this situation: When you specify a fixed Startup class using UseStartup <TStartup> (), it can also be based on the current program running environment, the method for loading the Startup class named "Configure {EnvironmentName}" in this format. For example, if the current execution environment of a program is a Staging environment, the ConfigureStaging method in the Startup class will be called instead of the Configure method.
V. Summary
Startup is flexible and can be used independently in the development, testing, and production environments.
Vi. References
Https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/environments