本文介绍了手动触发IOptionsMonitor<> .OnChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Core 2.1中,我使用IOptionsMonitor<>进行设置,以便在更改appSettings.json文件时可以成功获取事件.这样就可以了.

In ASP.NET Core 2.1, I use IOptionsMonitor<> and have it set up so I can successfully get events for when I change the appSettings.json file. So this is working.

我现在要做的是通过代码手动更改选项中的某些值,并触发所有监视器.这可能吗?

What I want to do now is to manually change some values in my options, through code, and have that trigger all my monitors. Is this possible?

推荐答案

对于IOptionsMonitor<Locations>,它仅更改内存中的值,而没有保存回appsettings.json.要解决此问题,您将需要实现自己的方法以将更改保存回appsettings.json.

For IOptionsMonitor<Locations>, it only changes the value in memory and did not save back to appsettings.json. For a workaround, you will need to implement your own method to save the changes back to appsettings.json.

  • 定义从IOptions

public interface IWritableOptions<out T> : IOptions<T> where T : class, new()
{
     void Update(Action<T> applyChanges);
}

  • 实现自己的WritableOptions

    public class WritableOptions<T> : IWritableOptions<T> where T : class, new()
    {
    private readonly IHostingEnvironment _environment;
    private readonly IOptionsMonitor<T> _options;
    private readonly IConfigurationRoot _configuration;
    private readonly string _section;
    private readonly string _file;
    
    public WritableOptions(
        IHostingEnvironment environment,
        IOptionsMonitor<T> options,
        IConfigurationRoot configuration,
        string section,
        string file)
    {
        _environment = environment;
        _options = options;
        _configuration = configuration;
        _section = section;
        _file = file;
    }
    
    public T Value => _options.CurrentValue;
    public T Get(string name) => _options.Get(name);
    
    public void Update(Action<T> applyChanges)
    {
        var fileProvider = _environment.ContentRootFileProvider;
        var fileInfo = fileProvider.GetFileInfo(_file);
        var physicalPath = fileInfo.PhysicalPath;
    
        var jObject = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(physicalPath));
        var sectionObject = jObject.TryGetValue(_section, out JToken section) ?
            JsonConvert.DeserializeObject<T>(section.ToString()) : (Value ?? new T());
    
        applyChanges(sectionObject);
    
        jObject[_section] = JObject.Parse(JsonConvert.SerializeObject(sectionObject));
        File.WriteAllText(physicalPath, JsonConvert.SerializeObject(jObject, Formatting.Indented));
        _configuration.Reload();
    }
    }
    

  • 配置IWritableOptions<T>

    public static class ServiceCollectionExtensions
    {
    public static void ConfigureWritable<T>(
        this IServiceCollection services,
        IConfigurationSection section,
        string file = "appsettings.json") where T : class, new()
    {
        services.Configure<T>(section);
        services.AddTransient<IWritableOptions<T>>(provider =>
        {
            var configuration = (IConfigurationRoot)provider.GetService<IConfiguration>();
            var environment = provider.GetService<IHostingEnvironment>();
            var options = provider.GetService<IOptionsMonitor<T>>();
            return new WritableOptions<T>(environment, options, configuration, section.Key, file);
        });
    }
    }
    

  • Startup

            services.ConfigureWritable<Locations>(Configuration.GetSection("Locations"));
    

  • 使用

  • Use

    public class OptionsController : Controller
    {
    private readonly IWritableOptions<Locations> _writableLocations;
    public OptionsController(IWritableOptions<Locations> writableLocations)
    {
        _writableLocations = writableLocations;
    }
    
    public IActionResult Change(string value)
    {
        _writableLocations.Update(opt => {
            opt.Name = value;
        });
        return Ok("OK");
    }
    }
    

  • 它将触发IOptionsMonitor<>.OnChange

    这篇关于手动触发IOptionsMonitor&lt;&gt; .OnChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 07-15 03:22