我将尝试创建自定义cinfig提供程序,该提供程序将从数据库中获取密钥。如手册中所述,我创建了这个提供程序,并且可以正常工作。所有键都在启动时加载,并且一切正常。

但是现在,我尝试使用IOptionsSnapshot并从db中重新加载密钥,以使它们发生更改。但是什么也没发生。

谁能告诉我作品出错了。这是我的代码:

    public class EFConfigProvider : ConfigurationProvider
{
    private DateTime lastLoaded;
    public EFConfigProvider(Action<DbContextOptionsBuilder> optionsAction)
    {
        OptionsAction = optionsAction;
        lastLoaded = DateTime.Now;
        ChangeToken.OnChange(
            () => Watch(),
            () => {
                Thread.Sleep(250);
                this.Load();
            });
    }

    public new IChangeToken GetReloadToken()
    {
        return Watch();
    }

    Action<DbContextOptionsBuilder> OptionsAction { get; }

    // Load config data from EF DB.
    public override void Load()
    {
        this.Data.Clear();

        var builder = new DbContextOptionsBuilder<ConfigContext>();
        OptionsAction(builder);

        using (var dbContext = new ConfigContext(builder.Options))
        {
            // Save Load Fact
            dbContext.SaveLoadFact();
            // Load Partners Settings
            GetPartners(dbContext);
        }
    }

    private IChangeToken Watch()
    {
        return new DatabaseChangeToken();
    }
}

public class DatabaseChangeToken : IChangeToken
{
    public bool HasChanged
    {
        get
        {
            return true;
        }
    }
    public bool ActiveChangeCallbacks => false;

    public IDisposable RegisterChangeCallback(Action<object> callback, object state) => EmptyDisposable.Instance;

    internal class EmptyDisposable : IDisposable
    {
        public static EmptyDisposable Instance { get; } = new EmptyDisposable();
        private EmptyDisposable() { }
        public void Dispose() { }
    }
}


我为开始工作所做的事情:


我在类EFConfigProvider中添加变量




private ConfigurationReloadToken _reloadToken = new ConfigurationReloadToken();



我添加构造函数




// Start Periodic task to refresh the DB
        PeriodicTask.Run(() =>
        {
            //Refresh();
            OnReload();
        }, TimeSpan.FromSeconds(reload));



我为periodTask添加类




public class PeriodicTask
{
    public static async Task Run(Action action, TimeSpan period, CancellationToken cancellationToken)
    {
        while (!cancellationToken.IsCancellationRequested)
        {
            await Task.Delay(period, cancellationToken);

            if (!cancellationToken.IsCancellationRequested)
                action();
        }
    }

    public static Task Run(Action action, TimeSpan period)
    {
        return Run(action, period, CancellationToken.None);
    }
}



在重新加载时添加方法




protected new void OnReload()
        {
            var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
            previousToken.OnReload();
        }



添加使用更改令牌的更改




  ChangeToken.OnChange(
                () => { return this._reloadToken; },
                () => {
                    Thread.Sleep(250);
                    this.Load();
                });

最佳答案

我有相同的要求,我找到了代码,但我尝试了同样的事情,但没有工作。

但是您的代码和其他一些调查将我引向了提示。

public class CustomConfigurationProvider : ConfigurationProvider
{
    private readonly string applicationName;
    private readonly bool reloadOnChange;
    private readonly IConfiguration configuration;

    public CustomConfigurationProvider(string applicationName, bool reloadOnChange)
    {
        this.applicationName = applicationName;
        this.reloadOnChange = reloadOnChange;

        if(reloadOnChange)
        {
        ChangeToken.OnChange(
            () => GetReloadToken(), // listener to token change
            () =>
            {
                Thread.Sleep(250);
                this.Load();
            });
        }
    }

    public override async void Load()
    {
        Data.Clear();

        Data = read data from database;

        if (Condition to check if data in database changed)
        {
            OnReload(); // This will create new token and trigger change so what is register in OnChange above will be called again which is this.Load()
        }
    }
}


我还引用了https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location



https://docs.microsoft.com/en-us/aspnet/core/fundamentals/primitives/change-tokens

希望这可以帮助。

关于c# - ChangeToken.OnChange不会在自定义配置提供程序上触发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48770143/

10-10 03:00