asp.net-core - reloadOnChange 在 .NET Core 2 中不起作用

标签 asp.net-core configuration appsettings asp.net-core-2.0

我有一个在 netcore2 下运行的 ASP.NET Core 2 项目。我正在重写 BuildWebHost 以添加新的 JSON 配置文件,如下所示:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile("mysettings.json", optional: false, reloadOnChange: true);
        })
        .UseStartup<Startup>()
        .Build();

但是,在 Controller 的 ActionFilter 中,我尝试像这样抓取它:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    var options = filterContext.HttpContext.RequestServices.GetService(typeof(IOptions<MySettings>));

    /// Do something with the options.
}

这些选项似乎仍然与应用程序启动时存在的选项相同。知道为什么他们不重新加载吗?

最佳答案

使用IOptionsSnapshot<>如果您需要它响应配置中的更改

关于asp.net-core - reloadOnChange 在 .NET Core 2 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46454201/

相关文章:

c++ - Libconfig 编辑配置值

c# - 在卸载期间(卸载前)从自定义安装程序获取对 AppSettings 的访问权限

c# - 如何诊断 dnx 中缺少的依赖项(或其他加载程序故障)?

asp.net-core - .NET Core RuntimeIdentifier 与 TargetFramework

windows - 如何为 Puppet 安装全局自定义提供程序?

c# - 为什么 appSettings 文件属性不允许父目录的路径?

c# - 有人可以提供快速的 App.config/Web.config 教程吗?

c# - UseHsts 不适用于 Net Core 2.1 网站

asp.net - 不明确的引用问题(Microsoft.AspNet.Identity 和 Microsoft.AspNet.Identity.Core)

java - 如何访问 WEB-INF 文件夹外的 jdbc 属性文件(用于 Spring Security 配置)?