C# Dotnet core Console appsettings.json 运行时重新加载

发布于 2025-01-11 18:52:43 字数 672 浏览 0 评论 0原文

我需要在控制台应用程序运行时更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2025-01-18 18:52:43

配置正在监视(通过 reloadOnChange: true)当前工作目录中的 appsettings.json 文件(通过 Directory.GetCurrentDirectory())。

如果进行调试,这是构建目录。

调试时不会反映对项目的 appsettings.json 文件(“主”副本)的编辑。相反,编辑构建目录中的副本。

The configuration is monitoring (via reloadOnChange: true) the appsettings.json file in the current working directory (via Directory.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.

软糯酥胸 2025-01-18 18:52:43

试试这个

public IConfiguration GetAppssetingsConfig()
{
 return new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
 }

try this

public IConfiguration GetAppssetingsConfig()
{
 return new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文