您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān).Net Core3.0如何配置Configuration的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
準備
.NET core和.NET項目配置上有了很大的改變,支持的也更加豐富了比如命令行,環(huán)境變量,內(nèi)存中.NET對象,設置文件等等。.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中制定的版本是否一致:然后修改即可
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);
}
}
將文件讀入配置時,會創(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";
輸出:
{
"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;
運行輸出:
感謝各位的閱讀!關(guān)于“.Net Core3.0如何配置Configuration”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。