介绍
本节为大家带来.net 6新增的configurationmanager,很多人好奇为啥要讲这个,读取加载配置信息都随手就来了,我们往下看一下。

翻译:这添加了 asp.net core 的新 webapplcation 和 webapplicationbuilder已经使用的类型,允许从配置(例如appsettings.json和dotnet_/aspnetcore_环境变量)中读取,同时仍然能够添加新的配置源,而无需显式重建配置。每次通过iconfigurationbuilder界面添加源时iconfiguration,都会立即自动更新。
回顾历史
我们在使用.net 5开发的时候,采用iconfigurationbuilder添加配置源。调用build()构建器读取每个配置源,并构建最终配置iconfigurationroot。
| 1 2 3 4 5 6 7 8 |
private static iconfigurationroot buildconfiguration()
{
var builder = new configurationbuilder()
.setbasepath(path.combine(directory.getcurrentdirectory(), "../mycompanyname.myprojectname.dbmigrator/"))
.addjsonfile("appsettings.json", optional: false);
return builder.build();
}
|
当然我们正常系统开发基本上不会自己去调用configurationbuilder或者调用build(),这都在.net core底部给我们完成了。
那么这个类型推出的意义是什么呢?
举个栗子use application id and x.509 certificate for non-azure-hosted apps,这是微软官方给出的案例,我来说明一下,配置 azure key vault 提供程序需要一个配置值, 那么我是先有鸡还是先有蛋呢——在构建配置之前无法添加配置源!。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public static ihostbuilder createhostbuilder(string[] args) =>
host.createdefaultbuilder(args)
.configureappconfiguration((context, config) =>
{
if (context.hostingenvironment.isproduction())
{
var builtconfig = config.build();
using var store = new x509store(storelocation.currentuser);
store.open(openflags.readonly);
var certs = store.certificates.find(
x509findtype.findbythumbprint,
builtconfig["azureadcertthumbprint"], false);
config.addazurekeyvault(new uri($"https://{builtconfig["keyvaultname"]}.vault.azure.net/"),
new clientcertificatecredential(builtconfig["azureaddirectoryid"], builtconfig["azureadapplicationid"], certs.oftype<x509certificate2>().single()),
new keyvaultsecretmanager());
store.close();
}
})
.configurewebhostdefaults(webbuilder => webbuilder.usestartup<startup>());
|
我们的步骤是:
- 初始化配置
- 调用iconfigurationbuilder.build()构建配置
- 从iconfigurationroot中检索所需的配置值
- 添加配置源
- 框架调用build(),生成最终应用程序配置。
这里我们调用了build()两次,这会产生什么问题呢?
configurationbuilder.build()每次调用会迭代所有源,加载提供者,并产生新的实例configurationroot.大家应该都懂读取文件所需的消耗吧。
新的实现
我们在使用configurationmanager时,当iconfigurationsource添加一个addjsonfile()调用,提供程序会立即加载,并更新配置。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
using var config = new configurationmanager();
config.addenvironmentvariables(prefix: "mycustomprefix_");
if (config["fileconfig"] == "enabled")
{
config.addjsonfile("myconfig.json", optional: true, reloadonchange: true);
}
string myvaluefromjson = config["jsonconfigvalue"];
public class configurationmanager
{
private void addsource(iconfigurationsource source)
{
lock (_providerlock)
{
iconfigurationprovider provider = source.build(this);
_providers.add(provider);
provider.load();
_changetokenregistrations.add(changetoken.onchange(() => provider.getreloadtoken(), () => raisechanged()));
}
raisechanged();
}
}
private void reloadsources()
{
lock (_providerlock)
{
disposeregistrationsandprovidersunsynchronized();
_changetokenregistrations.clear();
_providers.clear();
foreach (var source in _sources)
{
_providers.add(source.build(this));
}
foreach (var p in _providers)
{
p.load();
_changetokenregistrations.add(changetoken.onchange(() => p.getreloadtoken(), () => raisechanged()));
}
}
raisechanged();
}
|
注意:configurationmanager因为会任何源发生更改后必须删除所有内容并重新开始,遍历每个源,重新加载它们。如果你做了很多的配置源操纵的,那么如果使用configurationmanager会带来相反的效果.
configurationmanager适用于配置部分建造和、完全构建。
结语
请不要关心在使用.net 6的时候该使用configurationmanager还是configurationbuilder,在开发中根据需求去使用不同的加载方案才是最好的。
到此这篇文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/MrChuJiu/p/15591837.html








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。