In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how .net Core3.0 configures Configuration. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Prepare for
There have been great changes in the configuration of .NET core and .NET projects, with richer support such as command line, environment variables, in-memory .NET objects, settings files, and so on. Net projects we often put configuration information in webConfig or appConfig. Configure the relevant source code https://github.com/aspnet/Extensions; if you open the source code project, if you encounter the following error, do not encounter a direct skip.
Error prompt: error: The project file cannot be opened by the project system, because it is missing some critical imports or the referenced SDK cannot be found. Detailed Information:
Solution: check whether the locally installed sdk is consistent with the version developed in global.json: then modify it
To start a new Asp.net Core web application, the appsettings.json is created by default; when the application starts the build host, the CreateDefaultBuilder method is called, and the appsettings.json is loaded by default. The code is as follows: public static IHostBuilder CreateDefaultBuilder (string [] args) {var builder = new HostBuilder ()
Builder.UseContentRoot (Directory.GetCurrentDirectory ()); builder.ConfigureHostConfiguration (config = > {config.AddEnvironmentVariables (prefix: "DOTNET_"); if (args! = null) {config.AddCommandLine (args);}})
Builder.ConfigureAppConfiguration ((hostingContext, config) = > {var env = hostingContext.HostingEnvironment
Config.AddJsonFile ("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile ($"appsettings. {env.EnvironmentName} .json", optional: true, reloadOnChange: true)
If (env.IsDevelopment () & &! string.IsNullOrEmpty (env.ApplicationName)) {var appAssembly = Assembly.Load (new AssemblyName (env.ApplicationName)); if (appAssembly! = null) {config.AddUserSecrets (appAssembly, optional: true) }} use GetValue,GetSection,GetChildren to read appsettings.json key-value pairs. We open the appsettings.json file:
When you read the file into the configuration, a unique hierarchical key is created to save the configuration values:
Logging:LogLevel:Default
Logging:LogLevel:System
Logging:LogLevel:Microsoft
Logging:LogLevel:Microsoft.Hosting.Lifetime
AllowedHosts
Var jsonValue = $"AllowedHosts: {_ config [" AllowedHosts "]}" + "\ r\ n"; jsonValue + = "Logging:LogLevel:Default:" + _ config.GetValue ("Logging:LogLevel:Default") + "\ r\ n"
/ / GetSection returns IConfigurationSection If it does not match to return null / / jsonValue + = "- -" + _ config.GetSection ("Logging:LogLevel:System"); jsonValue + = "Logging:LogLevel:System:" + _ config.GetSection ("Logging:LogLevel:System"). Value+ "\ r\ n\ n"; var logSection = _ config.GetSection ("Logging:LogLevel"); var configurationSections = logSection.GetChildren () Foreach (var sections in configurationSections) {jsonValue + = $"{sections.Path}: {sections.Value}"; jsonValue + = "\ r\ n";} jsonValue + = "\ r\ n"
Output:
Configure the specified json file to bind to the class create a new json file-AAAppSettings.json {"AA": {"RabbitMqHostUrl": "rabbitmq://localhost:5672", "RabbitMqHostName": "localhost", "RabbitMqUserName": "admin", "RabbitMqPassword": "123"}}
Use the ConfigureAppConfiguration method to configure the specified json file public static IHostBuilder CreateHostBuilder (string [] args) = > Host.CreateDefaultBuilder (args) .ConfigreAppConfiguration ((hostingContext, config) = > {config.SetBasePath (Directory.GetCurrentDirectory ()); config.AddJsonFile ("AAAppSettings.json", optional: true, reloadOnChange: true);})
Use the bind method to bind to a newly created class, such as:
Public partial class AAConfig {public string RabbitMqHostUrl {get; set;} public string RabbitMqHostName {get; set;} public string RabbitMqUserName {get; set;} public string RabbitMqPassword {get; set;}} var aaConfig = new AAConfig (); _ config.GetSection ("AA") .Bind (aaConfig); jsonValue + = aaConfig.RabbitMqHostUrl + "\ r\ n"; jsonValue + = aaConfig.RabbitMqHostName + "\ r\ n"; jsonValue + = aaConfig.RabbitMqUserName + "\ r\ n" JsonValue + = aaConfig.RabbitMqPassword + "\ r\ n"; return jsonValue
Run the output:
Thank you for reading! This is the end of the article on "how to configure Configuration with .net Core3.0". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.