Startup processing in different environments and startup Processing

Source: Internet
Author: User

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

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.