I am developing a .Net 6.0 ASP.NET Core Web App in which appsettings.json
is edited for admin purpose after the app has started. The information to be edited looks like:
{
"Admin": {
"WhiteList": ["::1",.....]
},
....
which is modeled as
public class AdminOptions
{
public const string Admin = "Admin";
public string[] WhiteList { get; set; }
}
and configured in Program.cs
as follows:
...
ver builder = WebApplication.CreateBuilder(args);
builder.Services.Configuraion<AdminOptions>(
builder.Configuration.GetSection(AdminOptions.Admin));
AdminOptions
is constructor-injected to a Singleton service which looks like:
public class WhiteChecker: IWhiteChecker
{
private readonly IOptionsMonitor<AdminOptions> optionsMonitor;
public WhiteChecker(IOptionsMonitor<AdminOptions> monitor)
{
optionsMonitor = monitor;
}
.....[optionsMonitor.CurrentValue.WhiteList is consumed here].....
}
If appsettings.json
in the project root is edited, IOptionsMonitor.CurrentValue
returns the post-edit value of the pre-bound WhiteList
property in Admin
section, as expected.
However, if the one in AppContext.BaseDirectory
(which I believe is the right place) is edited, IOptionsMonitor
ignores.
I tried a change in WebApplicationBuilder.Environment.ContentRootPath
(which is set to the project root by default) to AppContext.BaseDirectory
before the WebApplicationBuilder
is built at Program.cs
, but the same result.
Is there any way to configure IOptionsMonitor
to respond to the change in appsettings.json
in Appcontext.BaseDirectory
? Or do I misunderstand anything?
CodePudding user response:
The issue sounds like a non-issue according to the documentation provided by Microsoft. I quote from this page:
During development, the content root defaults to the project's root directory. This directory is also the base path for both the app's content files and the Web root. Specify a different content root by setting its path when building the host. For more information, see Content root.
Same page, different anchor:
The CreateDefaultBuilder method:
- Sets the content root to the path returned by GetCurrentDirectory.
In Short...
What you see is the expected behavior: The project's root for development, and when deployed somewhere, whatever is set for working directory.