.Net Core3.0 配置Configuration的實(shí)現(xiàn)
準(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)文章
asp.net動(dòng)態(tài)加載用戶控件,關(guān)于后臺(tái)添加、修改的思考
一直以來,我都在思考,一些繁瑣的操作,比如我們一般的管理后臺(tái),很多都是數(shù)據(jù)的添加、修改與刪除,列表的操作,而且一般我們都是用.aspx文件去做的。2009-04-04Linq中ToList()和CopyToDataTable()用法詳解
這篇文章介紹了Linq中ToList()和CopyToDataTable()的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03ASP.Net分頁(yè)的分頁(yè)導(dǎo)航實(shí)例
本文介紹了ASP.Net分頁(yè)的分頁(yè)導(dǎo)航實(shí)例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-10-10在asp.net(c#)下實(shí)現(xiàn)調(diào)用cmd的方法
通常情況下我們會(huì)用到調(diào)用cmd.exe來實(shí)現(xiàn)一些命令,例如 ping ,等等2012-01-01教你Asp.net下使用mysql數(shù)據(jù)庫(kù)的步驟
近日,在項(xiàng)目中遇到了麻煩,客戶非要求使用mysql數(shù)據(jù)庫(kù),對(duì)于我從來么有使用過的人來說,很是頭疼,最后還是硬著頭皮弄好了。期間也遇到了各種各樣的問題,現(xiàn)在把他整理在此,希望對(duì)那些和我一樣從來沒有使用過的人,能快速入手2012-05-05ASP.NET框架中的數(shù)據(jù)綁定概要與數(shù)據(jù)綁定表達(dá)式的使用
數(shù)據(jù)綁定是ASP.NET中操作數(shù)據(jù)的基礎(chǔ)方式,這里我們暫時(shí)拋開.NET提供的控件,來從基礎(chǔ)上講解ASP.NET框架中的數(shù)據(jù)綁定概要與數(shù)據(jù)綁定表達(dá)式的使用:2016-06-06