欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.Net Core3.0 配置Configuration的實(shí)現(xiàn)

 更新時(shí)間:2020年10月26日 11:26:30   作者:李明成  
這篇文章主要介紹了.Net Core3.0 配置Configuration的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

準(zhǔn)備

.NET core和.NET項(xiàng)目配置上有了很大的改變,支持的也更加豐富了比如命令行,環(huán)境變量,內(nèi)存中.NET對(duì)象,設(shè)置文件等等。.NET項(xiàng)目我們常常把配置信息放到webConfig 或者appConfig中。配置相關(guān)的源碼https://github.com/aspnet/Extensions;如果打開源碼項(xiàng)目如果遇到以下錯(cuò)誤,未遇到直接跳過。

錯(cuò)誤提示: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:

解決辦法:查看本地安裝的sdk 與 global.json中制定的版本是否一致:然后修改即可

開始

新建個(gè)Asp.net Core web應(yīng)用程序系統(tǒng)默認(rèn)創(chuàng)建了appsettings.json ;在應(yīng)用啟動(dòng)生成主機(jī)時(shí)調(diào)用CreateDefaultBuilder方法,默認(rèn)會(huì)加載appsettings.json。代碼如下:

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);
          }
        }

利用GetValue,GetSection,GetChildren讀取appsettings.json 鍵值對(duì) 。我們打開appsettings.json文件:

將文件讀入配置時(shí),會(huì)創(chuàng)建一下唯一的分層健來保存配置值:

  • 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<string>("Logging:LogLevel:Default")+ "\r\n";
​
      //GetSection 返回IConfigurationSection;如果未匹配到 返回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";

配置指定json文件綁定至類

新建一個(gè)json文件-AAAppSettings.json

{
 "AA": {
  "RabbitMqHostUrl": "rabbitmq://localhost:5672",
  "RabbitMqHostName": "localhost",
  "RabbitMqUserName": "admin",
  "RabbitMqPassword": "123"
 }
}

使用ConfigureAppConfiguration方法配置指定的json文件

public static IHostBuilder CreateHostBuilder(string[] args) =>
      Host.CreateDefaultBuilder(args)
      .ConfigureAppConfiguration((hostingContext, config) =>
      {
        config.SetBasePath(Directory.GetCurrentDirectory());
        config.AddJsonFile("AAAppSettings.json", optional: true, reloadOnChange: true);
      })

使用bind方法綁定到新建的類上如:

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;

運(yùn)行輸出:

到此這篇關(guān)于.Net Core3.0 配置Configuration的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān).Net Core3.0 配置Configuration內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論