微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 更改* .json文件后IOptionsSnapshot不刷新

我按照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已被删除.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐