架构师_程序员_码农网

Retrieve password
Register

QQ登录

Just one step to get started

Search
View:6591|Reply: 4
打印 上一主题 下一主题

[ASP.NET]ASP.NET Core (X) Configuration Configuration Prioritization Details

[Copy link]
跳转到指定楼层
owner
发表于 2022-3-2 16:15:38| 看该作者回帖奖励|ReverseBrowse|Read Mode
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 server
Next: [Practical]Kirin system using YUM source to install OceanBase error solution
NET, only published in the process of practice, encountered technical difficulties, do not mislead others.
sofabed
Posted on 2022-3-2 19:59:19|Only View Author
Study study study 。。。。。
The code farmer network, only published in the process of practice, encountered technical difficulties, do not mislead others.
ugg boots sale
楼主 | 发表于 2023-3-29 18:25:09| 看该作者
Linux System Configuration Environment Variables

Temporary Settings



Permanent Settings
The code farmer network, only published in the process of practice, encountered technical difficulties, do not mislead others.
ugg boots sale
floorboards
owner|Posted on 2024-5-12 17:41:57|Only see this author
ASP.NET Core (xxviii) Application Confidentiality in Storage Development
h ttps:// www.itsvse.com/thread-10768-1-1.html
The code farmer network, only published in the process of practice, encountered technical difficulties, do not mislead others.
ugg boots sale
5#
owner|Posted on 2024-7-12 13:20:29|Only see this author
ASP.NET Core (XXI) Configure Options AddOptions and Configure Differences
h ttps:// www.itsvse.com/thread-10614-1-1.html
I'm not sure if you're going to be able to get the best out of this, but I'm sure you're going to be able to get the best out of it.
You need to log in before you can post back Log in | Register

This version of the integral rules

DISCLAIMER:
All software, programming materials or articles released by the code farmer network is limited to study and research purposes; shall not be used for commercial or illegal use of the above content, otherwise, all the consequences of the user please be responsible for their own. This site information from the network, copyright dispute has nothing to do with this site. You must completely remove the above content from your computer within 24 hours of downloading. If you like the program, please support the genuine software, buy registration and get better genuine service. If there is any infringement, please contact us by email to deal with it.

Mail To:help@itsvse.com

QQ| ( ICP备14021824号-2 )|Sitemap

GMT+8, 2024-9-19 04:18

Quick ReplyBack to topBack to list