我遵循了 hello world 示例表单 IOptionsSnapshot ,但是在文件 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();
}
}
最佳答案
IOptions 和 IOptionsSnapshot 的源代码看起来相同。这些接口(interface)在 OptionsManager 中有共同的实现。所以 OptionsSnapshot 不会重新加载选项。使用 IOptionsMonitor 代替 IOptionsSnapshot。您不需要订阅 OnChange 事件,只需访问 CurrentValue 属性即可。
UPD
自 Microsoft.Extensions.Options 2.x 发布以来,IOptionsSnapshot 已被删除。
关于c# - *.json 文件更改后 IOptionsSnapshot 不刷新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42751867/