[英]Cannot use IOptionsMonitor to detect changes in ASP.NET Core
I am working on Asp.Net Core app我正在开发 Asp.Net Core 应用程序
I want to change the configuration settings after running the application我想在运行应用程序后更改配置设置
I am using IOptionsMonitor, but it is not detecting changes我正在使用 IOptionsMonitor,但它没有检测到更改
In Startup.cs -> Configuration() method I have在 Startup.cs -> Configuration() 方法中我有
services.Configure<Config>(Configuration.GetSection("someConfig"));
In a different class where these config settings are read, I wrote something like在读取这些配置设置的不同 class 中,我写了类似
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>>();
But when I change the configuration file (Json File), the change is not detected, and someConfig does not change.但是当我更改配置文件(Json File)时,没有检测到更改,并且 someConfig 没有更改。
Config POCO class:配置 POCO class:
public class Config
{
public string name {get; set;}
//More getters and setters
}
Edit:编辑:
services.AddSingleton<ConfigHelpers>;
I am using a singleton object in which I am trying to read the config.我正在使用 singleton object 在其中尝试读取配置。 It works fine if its not a snigleton.
如果它不是一个 snigleton,它就可以正常工作。 Is there a way to change the config even in a singleton object?
即使在 singleton object 中,有没有办法更改配置?
in ConfigHelpers.cs在 ConfigHelpers.cs
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>();
since it is defined as singleton in Startup.cs, changes made to Config are not reflected.由于它在 Startup.cs 中定义为 singleton,因此对 Config 所做的更改不会反映。
I am working on Asp.Net Core app我正在开发 Asp.Net Core 应用程序
I want to change the configuration settings after running the application我想在运行应用程序后更改配置设置
I am using IOptionsMonitor, but it is not detecting changes我正在使用 IOptionsMonitor,但它没有检测到变化
In Startup.cs -> Configuration() method I have在 Startup.cs -> Configuration() 方法中我有
services.Configure<Config>(Configuration.GetSection("someConfig"));
In a different class where these config settings are read, I wrote something like在读取这些配置设置的另一个类中,我写了类似的东西
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>>();
But when I change the configuration file (Json File), the change is not detected, and someConfig does not change.但是当我更改配置文件(Json File)时,没有检测到更改,并且someConfig没有更改。
Config POCO class:配置 POCO 类:
public class Config
{
public string name {get; set;}
//More getters and setters
}
Edit:编辑:
services.AddSingleton<ConfigHelpers>;
I am using a singleton object in which I am trying to read the config.我正在使用一个单例对象,我试图在其中读取配置。 It works fine if its not a snigleton.
如果它不是 snigleton,它工作正常。 Is there a way to change the config even in a singleton object ?
即使在单例对象中,有没有办法更改配置?
in ConfigHelpers.cs在 ConfigHelpers.cs
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>();
since it is defined as singleton in Startup.cs, changes made to Config are not reflected.由于它在 Startup.cs 中定义为单例,因此不会反映对 Config 所做的更改。
I am working on Asp.Net Core app我正在开发 Asp.Net Core 应用程序
I want to change the configuration settings after running the application我想在运行应用程序后更改配置设置
I am using IOptionsMonitor, but it is not detecting changes我正在使用 IOptionsMonitor,但它没有检测到变化
In Startup.cs -> Configuration() method I have在 Startup.cs -> Configuration() 方法中我有
services.Configure<Config>(Configuration.GetSection("someConfig"));
In a different class where these config settings are read, I wrote something like在读取这些配置设置的另一个类中,我写了类似的东西
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>>();
But when I change the configuration file (Json File), the change is not detected, and someConfig does not change.但是当我更改配置文件(Json File)时,没有检测到更改,并且someConfig没有更改。
Config POCO class:配置 POCO 类:
public class Config
{
public string name {get; set;}
//More getters and setters
}
Edit:编辑:
services.AddSingleton<ConfigHelpers>;
I am using a singleton object in which I am trying to read the config.我正在使用一个单例对象,我试图在其中读取配置。 It works fine if its not a snigleton.
如果它不是 snigleton,它工作正常。 Is there a way to change the config even in a singleton object ?
即使在单例对象中,有没有办法更改配置?
in ConfigHelpers.cs在 ConfigHelpers.cs
var someConfig= serviceProvider.GetRequiredService<IOptionsMonitor<Config>();
since it is defined as singleton in Startup.cs, changes made to Config are not reflected.由于它在 Startup.cs 中定义为单例,因此不会反映对 Config 所做的更改。
To detect configuration changes, a listener must be registered to the IOptionsMonitor
by using its OnChange
method.要检测配置更改,必须使用其
OnChange
方法将侦听器注册到IOptionsMonitor
。
Example on a BackgroundService
: BackgroundService
上的示例:
// ...
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Threading;
public class MySingletonService : BackgroundService
{
private IDisposable _optionsChangedListener;
private MyOptions _myCurrentOptions;
public MySingletonService(IOptionsMonitor<MyOptions> optionsMonitor)
{
_optionsChangedListener = optionsMonitor.OnChange(MyOptionsChanged);
_myCurrentOptions = optionsMonitor.CurrentValue;
}
private void MyOptionsChanged(MyOptions newOptions, string arg2)
{
_myCurrentOptions = newOptions;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine(_myCurrentOptions.MyProperty);
await Task.Delay(1000, stoppingToken);
}
}
public override void Dispose()
{
_optionsChangedListener.Dispose();
base.Dispose();
}
}
public class MyOptions
{
public const string SectionKey = "MyOptions";
public string MyProperty { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.