今天在学习asp.net core 的配置文件相关知识时,了解到AddJsonFile的reloadOnChange参数设置为true则配置文件发生变化后,可以重新加载配置。因为没有这知识,之前做winform端自定义配置时,都是文件启动时加在一次配置,没法实时修改配置后生效。现在记录下来以便以后可以使用。
参考 码农阿宇-浅析 .Net Core中Json配置的自动更新
asp.net core 配置文件设置
#region Configure 用于定义请求管道中的中间件
/// <summary>
/// 用于定义请求管道中的中间件
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app,IHostingEnvironment env)
{
ConfigurationSet(env);
//...隐藏其他中间件
}
#region ConfigurationSet 设置配置文件
/// <summary>
/// 设置配置文件
/// </summary>
/// <param name="env"></param>
private static void ConfigurationSet(IHostingEnvironment env)
{
//配置文件:如果同个配置在多个配置文件中同时存在,则最后的配置文件里的配置生效
var _builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json",optional: true,reloadOnChange: true)//从json文件中取值,reloadOnChange:当配置文件发生变化后,是否重新加载配置
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",optional: true);//在开发环境中会使用“appsettings.Development.json”的配置文件,并用其中的值覆盖当前存在的其他值。
_builder.AddEnvironmentVariables();//从环境变量中取值
SysConfig.WebCongfig = _builder.Build();
}
#endregion
文件监听
using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
namespace LchCommon
{
public class PhysicalFilesWatcher
{
//=================== construct function ==================
#region construct construct
/// <summary>
/// construct
/// </summary>
public PhysicalFilesWatcher()
{
//FileSystemWatcher(string path, string filter):path:目录,filter:默认 *.*,
this.m_fileWatcher = new FileSystemWatcher("f:\\lch","lch.txt")
{
IncludeSubdirectories = true//是否监视子目录
};
this.m_fileWatcher.Created += new FileSystemEventHandler(this.OnChanged);
this.m_fileWatcher.Changed += new FileSystemEventHandler(this.OnChanged);
this.m_fileWatcher.Renamed += new RenamedEventHandler(this.OnRenamed);
this.m_fileWatcher.Deleted += new FileSystemEventHandler(this.OnChanged);
this.m_fileWatcher.Error += new ErrorEventHandler(this.OnError);
this.m_fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size;//监听类型
this.m_fileWatcher.EnableRaisingEvents = true;//开启监听
}
#endregion
//=================== private fields ======================
#region fields
private FileSystemWatcher m_fileWatcher;
#endregion
//=================== event metod =========================
#region OnError 出错
/// <summary>
/// 出错
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnError(object sender,ErrorEventArgs e)
{
Debug.WriteLine("OnError");
}
#endregion
#region OnRenamed 重命名
/// <summary>
/// 重命名
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnRenamed(object sender,RenamedEventArgs e)
{
Debug.WriteLine("OnRenamed");
}
#endregion
#region OnChanged 改变事件
/// <summary>
/// 改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnChanged(object sender,FileSystemEventArgs e)
{
Debug.WriteLine("OnChanged");
Debug.WriteLine(JsonConvert.SerializeObject(e));
}
#endregion
}
}
触发修改事件的参数
参考 [码农阿宇-浅析 .Net Core中Json配置的自动更新]