ASP.NET Core (X) Configuration Configuration Prioritization Currently Offline
- ASP.NET
- 16373
|
When an ASP.NET Core project is started, the default execution sequence is: Host -> Read Configuration -> Log Settings -> Register Services (DI) -> Add Middleware -> WebHost Listening -> Backend Work Startup.
The loading and reading of the configuration is at the top of the startup process. Microsoft documentation on configuration in ASP.NET Core: https: //docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0
Review:
Middleware in ASP.NET Core (IX) - Middleware Explained h ttps:// www.itsvse.com/thread-9647-1-1.html
ASP.NET Core Middleware Explained h ttps:// www.itsvse.com/thread-8126-1-1.html
ASP.NET Core (8) - Swagger UI default parameter pitfalls h ttps:// www.itsvse.com/thread-9640-1-1.html
ASP.NET Core (VII) - Deep Dive into the Framework Source Code h ttps:// www.itsvse.com/thread-9601-1-1.html
ASP.NET Core (VI) DI Manually Getting Injected Objects h ttps:// www.itsvse.com/thread-9595-1-1.html
ASP.NET Core (V) CAP-based Distributed Transactions h ttps:// www.itsvse.com/thread-9593-1-1.html
ASP.NET Core (IV) of the filter unified ModelState model validation h ttps:// www.itsvse.com/thread-9589-1-1.html
ASP.NET Core (III) Using ActivatorUtilities to Dynamically Create Instances h ttps:// www.itsvse.com/thread-9488-1-1.html
ASP.NET Core (II) Self-Restarting Applications through Code h ttps:// www.itsvse.com/thread-9480-1-1.html
ASP.NET Core (I) Using Redis Cache h ttps:// www.itsvse.com/thread-9393-1-1.html The Host.CreateDefaultBuilder method provides the default configuration for the application in the following order:
- ChainedConfigurationProvider: adds an existing As a source. In the default configuration example, add the host configuration and set it as the first source for the application configuration.
- Use appsettings.json for appsettings.json.
- Use JSON Configuration Provider via appsettings..json. For example, appsettings.Production.json and appsettings.Development.json.
- Application secrets when the application is running in the environment.
- Use environment variables to configure the provider program to make it available via environment variables.
- Use the command line to configure the provider program to provide it via command line arguments.
The source code is shown below:
Source code address: https: //github.com/dotnet/extensions/blob/release/3.1/src/Hosting/Hosting/src/Host.cs
As you can see from the code, the program gets the configuration in order of priority: appsettings.json -> appsettings.environment.json -> environment variables -> command line parameters. Let's test based on the priority.
Create a new console method to return all the configuration information with the following code:
First, the appsettings.json configuration file, as follows:
Create a new appsettings.Test.json configuration as follows:
Try to start the project and check the configuration of WebConfig:Name and WebConfig:Date as shown below:
{"Key": "WebConfig:Name", "Value": "itsvse.com"},{"Key": "WebConfig:Date". "Value": "2021"}
Find the Properties -> launchSettings.json file and modify the ASPNETCORE_ENVIRONMENT environment configuration to Test as follows:
At this point , the program will read the appsettings.Test.json configuration, try to restart the project, and found that WebConfig:Name has been overwritten, as shown below:
{"Key": "WebConfig:Name", "Value": "itsvse.com test"},{"Key": "WebConfig:Date& quot;, "Value": "2021"}
Modify the launchSettings.json file again to set the value of WebConfig:Name via environment variable with the following code:
Note: To change the value of WebConfig:Name by environment variable, the variable name is: WebConfig__Name ( separated by double underscores ).
Try to restart the project and find that the value of WebConfig:Name has been overwritten by the value set by the environment variable as shown below:
Try to change the value of the default configuration via the command line, start the command as follows:
As shown below:
Use practice to test the priority of the configuration key value, end.
|
Previous article:CentOS 7 prohibit an IP access to the serverNext: [Practical]Kirin system using YUM source to install OceanBase error solution
|