思想汇报2012,差强人意是什么意思,公益海报设计大赛
var builder = new configurationbuilder() .addjsonfile($"appsettings.json", true, true);
you are creating file watchers, every time you access an setting. the 3rd parameter is reloadonchange.
you have to make sure,
var configuration = builder.build()
is only called once in your application and store it in a place where you can access it (preferably avoid static fields for it).
or just disable the file watcher.
var builder = new configurationbuilder() .addjsonfile($"appsettings.json", true, false);
or cleaner:
var builder = new configurationbuilder() .addjsonfile($"appsettings.json", optional: true, reloadonchange: false);
best way is to abstract hat behind an interface and use dependency injection.
public interface iconfigurationmanager { t getappconfig<t>(string key, t defaultvalue = default(t)); } public class configurationmanager : iconfigurationmanager { private readonly iconfigurationroot config; public configurationmanager(iconfigurationroot config) => this.config ?? throw new argumentnullexception(nameof(config)); public t getappconfig<t>(string key, t defaultvalue = default(t)) { t setting = (t)convert.changetype(configuration[key], typeof(t)); value = setting; if (setting == null) value = defaultvalue; } }
then instantiate and register it
services.addsingleton<iconfigurationmanager>(new configurationmanager(this.configuration));
and inject it into your services via constructor
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
asp.net搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论