无为清净楼资源网 Design By www.qnjia.com
准备
.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中制定的版本是否一致:然后修改即可
开始
新建个Asp.net Core web应用程序系统默认创建了appsettings.json ;在应用启动生成主机时调用CreateDefaultBuilder方法,默认会加载appsettings.json。代码如下:
public static IHostBuilder CreateDefaultBuilder(string[] args) { var builder = new HostBuilder(); "DOTNET_"); if (args != null) { config.AddCommandLine(args); } }); "appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true); "text-align: center">将文件读入配置时,会创建一下唯一的分层健来保存配置值:
- 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"; "---" + _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;
运行输出:
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年10月02日
2024年10月02日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】