c# – 更改* .json文件后IOptionsSnapshot不刷新
发布时间:2020-12-15 21:05:35 所属栏目:百科 来源:网络整理
导读:我按照hello world示例表单 IOptionsSnapshot进行了操作,但在更改和保存文件config.json后,内容未刷新. 我的开发环境: 1.VS 2017 2 .csproj文件如下 PropertyGroup TargetFrameworknetcoreapp1.1/TargetFramework PreserveCompilationContexttrue/PreserveC
我按照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的源代码看起来是一样的.该接口在
OptionsManager中具有通用实现.因此OptionsSnapshot不会重新加载选项.而不是IOptionsSnapshot,使用IOptionsMonitor.您无需订阅OnChange事件,只需访问CurrentValue属性即可.
UPD自Microsoft.Extensions.Options 2.x发布IOptionsSnapshot已被删除. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |