2017-03-12 10 views
4

IOptionsSnapshotのhello worldの例のフォームに従いましたが、ファイルconfig.jsonが変更されて保存された後にコンテンツがリフレッシュされません。* .jsonファイルの変更後にIOptionsSnapshotがリフレッシュされない

私のdevの環境:以下

<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> 

以下

1.VS 2017

2 .csprojファイルが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

正しいファイルを変更していますか?ベースパスが現在のディレクトリで、configをコピーしている場合は、ビルドフォルダ内のパスを変更する必要がありますか?ちょっとした考え! – Mardoxx

+0

VS内にあるconfig.jsonファイルは1つだけです。私はそれを確認した。 – Pingpong

答えて

0

からコードIOptionsのソースコードですIOptionsSnapshotは同じように見えます。そのインターフェイスはOptionsManagerに共通の実装があります。したがって、OptionsSnapshotはオプションをリロードしません。 IOptionsSnapshotの代わりに、IOptionsMonitorを使用します。 OnChangeイベントを購読する必要はなく、CurrentValueプロパティにアクセスするだけです。

UPD Microsoft.Extensions.Options 2.xリリースのIOptionsSnapshotが削除されています。

関連する問題