IOptionsSnapshot在*.json文件更改后不会刷新

5

我按照IOptionsSnapshot的hello world示例进行了操作,但是在文件config.json更改并保存后,内容没有被更新。

我的开发环境:

1. VS 2017

2. 下面是我的 .csproj 文件

  <PropertyGroup>
        <TargetFramework>netcoreapp1.1</TargetFramework>
        <PreserveCompilationContext>true</PreserveCompilationContext>
        <AssemblyName>UsingOptions</AssemblyName>
        <OutputType>Exe</OutputType>
        <PackageId>UsingOptions</PackageId>
        <RuntimeFrameworkVersion>1.0.4</RuntimeFrameworkVersion>
        <PackageTargetFallback>$(PackageTargetFallback);dotnet5.6;portable-net45+win8</PackageTargetFallback>
      </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
  </ItemGroup>

以下是来自IOptionsSnapshot的代码:
config.json:
{
  "Time": {
    "Message": "Hello "
  }
}


public class TimeOptions
{
    // Records the time when the options are created.
    public DateTime CreationTime { get; set; } = DateTime.Now;

    // Bound to config. Changes to the value of "Message"
    // in config.json will be reflected in this property.
    public string Message { get; set; }
}

public class Controller
{
    public readonly TimeOptions _options;

    public Controller(IOptionsSnapshot<TimeOptions> options)
    {
        _options = options.Value;
    }

    public Task DisplayTimeAsync(HttpContext context)
    {
        return context.Response.WriteAsync(_options.Message + _options.CreationTime);
    }
}

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            // reloadOnChange: true is required for config changes to be detected.
            .AddJsonFile("config.json", optional: false, reloadOnChange: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IConfigurationRoot Configuration { get; set; }

    public void Configure(IApplicationBuilder app)
    {
        // Simple mockup of a simple per request controller that writes
        // the creation time and message of TimeOptions.
        app.Run(DisplayTimeAsync);
    }

    public void ConfigureServices(IServiceCollection services)
    {
        // Simple mockup of a simple per request controller.
        services.AddScoped<Controller>();

        // Binds config.json to the options and setups the change tracking.
        services.Configure<TimeOptions>(Configuration.GetSection("Time"));
    }

    public Task DisplayTimeAsync(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        return context.RequestServices.GetRequiredService<Controller>().DisplayTimeAsync(context);
    }

    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();
        host.Run();
    }
}

1
你是否在修改正确的文件?如果基础路径是当前目录,并且你已经将其复制到配置中,那么你应该修改构建文件夹中的文件吗?只是一个想法! - Mardoxx
只有一个 config.json 文件,它在 VS 内部。我已经检查过了。 - Pingpong
1个回答

0

IOptions和IOptionsSnapshot的源代码看起来一样。这些接口在OptionsManager中有共同的实现。因此,OptionsSnapshot不会重新加载选项。请使用IOptionsMonitor而不是IOptionsSnapshot。您无需订阅OnChange事件,只需访问CurrentValue属性即可。

更新 自Microsoft.Extensions.Options 2.x版本发布以来,已删除IOptionsSnapshot。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接