.Net Core通过强类型绑定配置文件实现热更新

首先在Startup文件的ConfigureServices方法中初始化配置文件进行强类型绑定

var systemPath = new SystemPath();
Configuration.Bind("SystemPath", systemPath);
GlobalConfig.systemPath = systemPath;
public class GlobalConfig
    {
        public static SystemPath systemPath;
    }

然后使用 ChangeToken.OnChange 进行热更新

ChangeToken.OnChange(() => Configuration.GetReloadToken(), () =>
            {
                var config = new SystemPath();
                Configuration.Bind("SystemPath", config);
                GlobalConfig.systemPath = config;
            });

读取配置获取最新的路径

var path = GlobalConfig.systemPath.FilePath;

猜你喜欢

转载自www.cnblogs.com/ZhengHengWU/p/13197874.html