您好,登录后才能下订单哦!
这篇文章给大家分享的是有关.Net Core3.0如何配置Configuration的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
准备
.NET core和.NET项目配置上有了很大的改变,支持的也更加丰富了比如命令行,环境变量,内存中.NET对象,设置文件等等。.NET项目我们常常把配置信息放到webConfig 或者appConfig中。配置相关的源码https://github.com/aspnet/Extensions;如果打开源码项目 如果遇到以下错误,未遇到直接跳过。
错误提示: 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中制定的版本是否一致:然后修改即可
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);
}
}
将文件读入配置时,会创建一下唯一的分层健来保存配置值:
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";
输出:
{
"AA": {
"RabbitMqHostUrl": "rabbitmq://localhost:5672",
"RabbitMqHostName": "localhost",
"RabbitMqUserName": "admin",
"RabbitMqPassword": "123"
}
}
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;
运行输出:
感谢各位的阅读!关于“.Net Core3.0如何配置Configuration”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。