华域联盟 .Net .Net Core3.0 配置Configuration的实现

.Net Core3.0 配置Configuration的实现

.Net Core3.0 配置Configuration的实现
 更新时间:2020年10月26日 11:26:30   作者:李明成  

这篇文章主要介绍了.Net Core3.0 配置Configuration的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

准备
.NET core和.NET项目配置上有了很大的改变,支持的也更加丰富了比如命令行,环境变量,内存中.NET对象,设置文件等等。.NET项目我们常常把配置信息放到webConfig 或者appConfig中。配置相关的源码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中制定的版本是否一致:然后修改即可

开始
新建个Asp.net Core web应用程序系统默认创建了appsettings.json ;在应用启动生成主机时调用CreateDefaultBuilder方法,默认会加载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 键值对 。我们打开appsettings.json文件:

将文件读入配置时,会创建一下唯一的分层健来保存配置值:

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文件绑定至类
新建一个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;

运行输出:

到此这篇关于.Net Core3.0 配置Configuration的实现的文章就介绍到这了,更多相关.Net Core3.0 配置Configuration内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

您可能感兴趣的文章:如何在ASP.NET Core 的任意类中注入ConfigurationC# 添加对System.Configuration.dll文件的引用操作matplotlib运行时配置(Runtime Configuration,rc)参数rcParams解析mybatis的Configuration详解详解@ConfigurationProperties实现原理与实战@ConfigurationProperties绑定配置信息至Array、List、Map、Bean的实现详解配置类为什么要添加@Configuration注解Spring @Configuration注解及配置方法Springboot @Configuration @bean注解作用解析SpringBoot @ConfigurationProperties使用详解继承WebMvcConfigurationSupport后自动配置不生效及如何配置拦截器解析SpringBoot @EnableAutoConfiguration的使用Spring中基于Java的配置@Configuration和@Bean用法详解@Configuration与@Component作为配置类的区别详解.NET Core 3.0之创建基于Consul的Configuration扩展组件SpringBoot 中 AutoConfiguration的使用方法MyBatis异常-Property ''configLocation'' not specified, using default MyBatis ConfigurationSpring源码解析之Configuration

.Net
Core3.0
Configuration

相关文章
C# XML操作 代码大全(读XML,写XML,更新,删除节点,与dataset结合等C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等),以下就是操作XML的所有方法,相信可以满足很大一部份的使用了。 2009-06-06
.NET CORE中使用AutoMapper进行对象映射的方法这篇文章主要给大家介绍了关于.NET CORE中使用AutoMapper进行对象映射的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.NET CORE具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧 2019-04-04
asp.net传多个值到其它页面的具体实现在页面之间的跳转,经常会用到传值,其中可能会传递多个值,下面为大家介绍下asp.net传多个值到其它页面的方法,需要的朋友可以参考下 2014-02-02
关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍本篇文章小编将为大家介绍,关于.NET/C#/WCF/WPF 打造IP网络智能视频监控系统的介绍。需要的朋友参考下 2013-04-04
Element NavMenu导航菜单的使用方法这篇文章主要介绍了Element NavMenu导航菜单的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 2020-07-07
在Apache环境下成功的运行ASP.NET的注意事项在Apache环境下成功的运行ASP.NET的注意事项... 2007-08-08
ASP.Net不执行问题一解 2008-02-02
详解ASP.NET Core 中间件之压缩、缓存本篇文章主要介绍了ASP.NET Core 中间件之压缩、缓存,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
2016-12-12
已有打开的与此命令相关联的DataReader,必须首先将它关闭。对于此今天与大家分享一下 已有打开的与此命令相关联的DataReader,必须首先将它关闭。这个异常的个人理解 2012-01-01
asp.net(c#)做一个网页数据采集工具最近做一个网站,该网站需要添加4000多 产品信息,如果用人工方法去别的网站copy那至少要花费半月时间才能完成,所以我个办法使用c#作出来了一个网页数据采集软件. 2009-12-12

最新评论

本文由 华域联盟 原创撰写:华域联盟 » .Net Core3.0 配置Configuration的实现

转载请保留出处和原文链接:https://www.cnhackhy.com/5042.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者:

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部