C# Dotnet core Console appsettings.json 运行时重新加载
我需要在控制台应用程序运行时更改 appsettings.json 。 我用来加载 appsettings.json 的代码仅在启动时加载 appsettings.json 并且一旦应用程序运行它就不会刷新。有人可以帮我解决这个问题吗?
public IConfigurationRoot GetAppssetingsConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
configuration.Reload();
return configuration;
}
我期望的是,每次调用上述函数时,它都会读取当时的 appsettings.json,但这并没有发生。 感谢您的帮助
I have a need where I may change appsettings.json while my Console app is running.
The code I am using to load appsettings.json is only loads the appsettings.json at startup and it never refreshes once the app is running. Can some one help me figure this out pls?
public IConfigurationRoot GetAppssetingsConfig()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
configuration.Reload();
return configuration;
}
What I am expecting is that each time when I call the above function, it reads the appsettings.json at that time, however this is not happening.
Thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
配置正在监视(通过
reloadOnChange: true
)当前工作目录中的 appsettings.json 文件(通过Directory.GetCurrentDirectory()
)。如果进行调试,这是构建目录。
调试时不会反映对项目的 appsettings.json 文件(“主”副本)的编辑。相反,编辑构建目录中的副本。
The configuration is monitoring (via
reloadOnChange: true
) the appsettings.json file in the current working directory (viaDirectory.GetCurrentDirectory()
).This is the build directory, if debugging.
Edits to the project's appsettings.json file (the "master" copy) won't be reflected while debugging. Instead, edit the copy in the build directory.
试试这个
try this