.Net Core3.0 配置Configuration的實現(xiàn)
準備
.NET core和.NET項目配置上有了很大的改變,支持的也更加豐富了比如命令行,環(huán)境變量,內(nèi)存中.NET對象,設(shè)置文件等等。.NET項目我們常常把配置信息放到webConfig 或者appConfig中。配置相關(guān)的源碼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應(yīng)用程序系統(tǒng)默認創(chuàng)建了appsettings.json ;在應(yīng)用啟動生成主機時調(diào)用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文件:

將文件讀入配置時,會創(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文件綁定至類
新建一個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;
運行輸出:

到此這篇關(guān)于.Net Core3.0 配置Configuration的實現(xiàn)的文章就介紹到這了,更多相關(guān).Net Core3.0 配置Configuration內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET MVC4中使用Html.DropDownListFor的方法示例
這篇文章主要介紹了ASP.NET MVC4中使用Html.DropDownListFor的方法,結(jié)合實例形式分析了控制器數(shù)據(jù)源及Html.DropDownListFor顯示操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
詳解.NET中string與StringBuilder在字符串拼接功能上的比較
string與StringBuilder的在字符串拼接時執(zhí)行效率上有差異,這篇文章主要介紹了詳解.NET中string與StringBuilder在字符串拼接功能上的比較,感興趣的小伙伴們可以參考一下2018-11-11
Extjs4.1.x 框架搭建 采用Application動態(tài)按需加載MVC各模塊完美實現(xiàn)
中午的時候發(fā)了第一篇 Extjs4.1.x 框架搭建 采用Application動態(tài)按需加載MVC各模塊,發(fā)現(xiàn)實現(xiàn)上還是有問題,本文將提供詳細的完美方案2012-11-11
Visual Studio中調(diào)試 .NET源代碼的實現(xiàn)步驟
在調(diào)試 .NET 應(yīng)用程序時,有時你可能需要查看其他人的源代碼,本文主要介紹了Visual Studio中調(diào)試 .NET源代碼的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-03-03
.Net?Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟
最近在使用調(diào)度程序創(chuàng)建簡單的服務(wù),該服務(wù)將執(zhí)行一些重復(fù)的IO操作,使用的是Coravel調(diào)度庫,下面這篇文章主要給大家介紹了關(guān)于.Net?Core使用Coravel實現(xiàn)任務(wù)調(diào)度的完整步驟,需要的朋友可以參考下2022-08-08

