public class Startup
    {
        private ConfigurationReloadToken _changeToken = new ConfigurationReloadToken();
        private byte[] _configFileHash = new byte[20];

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public void ConfigureServices(IServiceCollection services)
        {
            ...
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration config)
        {
            ChangeToken.OnChange(
               () => config.GetReloadToken(),
               () =>
               {
                   byte[] configFileHash = ComputeHash("appSettings.json");

                   if (!_configFileHash.SequenceEqual(configFileHash))
                   {
                       Log.Information("Configuration changed");
                       _configFileHash = configFileHash;
                   }
                   var previousToken =
                       Interlocked.Exchange(ref _changeToken, new ConfigurationReloadToken());
                   previousToken.OnReload();
               });
        }
        private byte[] ComputeHash(string file)
        {
            if (File.Exists(file))
            {
                using (var fs = File.OpenRead(file))
                {
                    return System.Security.Cryptography.SHA1.Create().ComputeHash(fs);
                }
            }
            else
            {
                return new byte[20];
            }
        }
    }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-28
  • 2021-10-06
  • 2021-09-11
  • 2022-12-23
相关资源
相似解决方案