.NET Core技术研究-最实用最常用的配置读取方式
2021-03-23 11:27
标签:this 划线 指令 order nts sha lis 参数配置 cti
升级ASP.NET Core后,配置的读取是第一个要明确的技术。原先的App.Config、Web.Config、自定义Config在ASP.NET Core中如何正常使用。有必要好好总结整理一下,相信大家都会用到。 首先,看一下ASP.NET Core中的配置提供程序(Configuration Providers): 一、配置提供程序(Configuration Providers) ASP.NET Core 使用一个或多个配置提供程序来读取配置: 我们通过下面的代码,输出配置提供程序的加载顺序: 输出有5个: 接下来我们我们重点介绍“应用程序配置文件”,“环境变量”,“命令行参数”,“app.config” 这四种最常用的配置读取方式,通过代码的方式,示例给大家: 二、读取应用程序设置文件appsettings.json 我们使用ASP.NET Core工程中默认的appsettings.json文件 通过代码读取配置 实际输出: allowedHosts:* defaultLogLevel:Information 如果想读取整个的LogLevel对象,如何实现? 新建LogLevel类和Logging类 读取Logging配置示例代码: 上述代码中,对Lifetime属性的设置,通过以下方式实现,Bind的方式因为key匹配的问题,无法完成匹配。 Configuration["Logging:LogLevel:Microsoft.Hosting.Lifetime"]; 这个地方补充一个带环境类型的应用设置文件的价值顺序:比如说按环境分appsettings.json文件 默认的JsonConfigurationProvider ,按以下顺序加载 appsettings.json文件: ① appsettings.json 关于appsettings.Environment.json,Environment的设置主页在Program时指定的变量: 三、环境变量读取 按照配置的加载顺序,EnvironmentVariablesConfigurationProvider从环境变量中读取配置信息,在appsettings.json和Secret manager读取配置之后。 这个地方有个分隔符的注意点,因为 :并不是在所有平台上都支持,建议统一使用__(双下划线),运行时会将__统一替换为: 先通过以下命令,设置环境变量: 代码中读取环境变量的配置 修改Startup中Configure方法:读取配置并输出 四、命令行参数读取 命令行配置提供程序CommandLineConfigurationProvider,将在以下配置源之后从命令行参数键值对加载配置: 继续使用第三章中的示例工程,新建CMD命令行,输入以下dotnet run指令: 五、app.config读取 这个场景最大的作用就是兼容原有ASP.NET Web.Config文件! 首先添加Nuget引用:System.Configuration.ConfigurationManager 新增app.config文件: 使用原有ConfigurationManager读取配置: 以上就是ASP.NET Core常用的“应用程序配置文件”,“环境变量”,“命令行参数”,“app.config” 配置读取方式,分享给大家。 周国庆 2020/4/1 .NET Core技术研究-最实用最常用的配置读取方式 标签:this 划线 指令 order nts sha lis 参数配置 cti 原文地址:https://www.cnblogs.com/lonelyxmas/p/12688643.html
var configRoot = (IConfigurationRoot)Configuration;
foreach (var provider in configRoot.Providers.ToList())
{ Debug.WriteLine(provider.ToString() + "\n");
}
{ "Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
var allowedHosts = Configuration["AllowedHosts"];
var defaultLogLevel = Configuration["Logging:LogLevel:Default"];
Debug.WriteLine("allowedHosts:" + allowedHosts + "\n");
Debug.WriteLine("defaultLogLevel:"+defaultLogLevel + "\n");
[DataContract] public class LogLevel
{
[DataMember(Name = "Default")]
public string Default { get; set; }
[DataMember(Name = "Microsoft")]
public string Microsoft { get; set; }
[DataMember(Name ="Microsoft.Hosting.Lifetime")]
public string MicrosoftHostingLifetime { get; set; }
}
[DataContract]
public class Logging
{
[DataMember]
public LogLevel LogLevel { get; set; }
}
Logging logConfig = new Logging();
Configuration.GetSection("Logging").Bind(logConfig);
var lifetime = Configuration["Logging:LogLevel:Microsoft.Hosting.Lifetime"];
logConfig.LogLevel.MicrosoftHostingLifetime = lifetime;
② appsettings.Environment.json,例如appsettings.Development.json ,appsettings.Production.json
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseEnvironment("Development");
webBuilder.UseStartup
});
}
set Key1="Value1"
set Logging__LogLevel__Customer=Information
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostBuilder, config) =>
{
config.AddEnvironmentVariables();
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseEnvironment("Development");
webBuilder.UseStartup
});
var key1Value = Configuration["Key1"];
var logLevel = Configuration["Logging:LogLevel:Customer"];
Console.WriteLine("key1Value:" + key1Value + "\n");
Console.WriteLine("logLevel:" + logLevel + "\n");

dotnet run Key1="Value1" Logging:LogLevel:Customer="Information"



xml version="1.0" encoding="utf-8"?>
configuration>
appSettings>
add key="ConfigKey1" value="Value" />
appSettings>
configuration>

var value = System.Configuration.ConfigurationManager.AppSettings["ConfigKey1"];
Console.WriteLine("ConfigKey1:" + value + "\n");

上一篇:ASP.NET Core技术研究-探秘Host主机启动过程
下一篇:初识CSS3