zoukankan      html  css  js  c++  java
  • .net core 3.1 配置文件立即更新

    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];
                }
            }
        }
    
  • 相关阅读:
    BZOJ3790 神奇项链
    Tarjan求Lca
    Manacher算法--Poj3974
    [Usaco2015 Feb]Censoring
    扩展Kmp
    Ural1297 最长回文串
    [Usaco2007 Dec] 队列变换
    Pku2774 最长公共子串
    Spoj 694 Distinct Substrings
    Pku1734 Musical Theme 不可重叠最长重复子
  • 原文地址:https://www.cnblogs.com/rsls/p/12586259.html
Copyright © 2011-2022 走看看