/// <summary> /// Perform an action when configuration changes. Note this requires configuration sources to be added with /// `reloadOnChange` enabled /// </summary> /// <param name="config">Configuration to watch for changes</param> /// <param name="action">Action to perform when <paramref name="config"/> is changed</param> public static void OnChange(this IConfiguration config, Action action) { // IConfiguration's change detection is based on FileSystemWatcher, which will fire multiple change // events for each change - Microsoft's code is buggy in that it doesn't bother to debounce/dedupe // https://github.com/aspnet/AspNetCore/issues/2542 var debouncer = new Debouncer(TimeSpan.FromSeconds(2)); ChangeToken.OnChange <object>(config.GetReloadToken, _ => debouncer.Debounce(action), null); }
public CompressedFileProvider(string filePath, string rootFolder) { var debouncer = new Debouncer(TimeSpan.FromSeconds(2)); this.filePath = filePath.ThrowIfEmpty(nameof(filePath)); this.rootFolder = rootFolder.ThrowIfNull(nameof(rootFolder)); this.cache = new ConcurrentDictionary <string, IFileInfo>(); ProcessZip(); var actualPath = Path.GetFullPath(filePath); fileWatcher = new FileSystemWatcher(); fileWatcher.Path = Path.GetDirectoryName(actualPath); fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName; fileWatcher.Filter = Path.GetFileName(actualPath); fileWatcher.Changed += (s, e) => debouncer.Debounce(() => ProcessZip()); fileWatcher.Created += (s, e) => debouncer.Debounce(() => ProcessZip()); fileWatcher.Renamed += (s, e) => debouncer.Debounce(() => ProcessZip()); fileWatcher.EnableRaisingEvents = true; physicalFileProvider = new PhysicalFileProvider(fileWatcher.Path); }