Ocelot dynamic update configuration files

       This article is intended by ocelot understand asp.net core dynamically updated configuration files.

A configuration file with the object bound

     UseOcelot method executes when calling profile object creation, CreateConfiguration methods are important objects IOptionsMonitor <FileConfiguration> by 

fileConfig.onchange register a callback method to change the repository. Implement updating of the memory of the object FileConfiguration.
   public static async Task<IApplicationBuilder> UseOcelot(this IApplicationBuilder builder, OcelotPipelineConfiguration pipelineConfiguration)
        {
            var configuration = await CreateConfiguration(builder);
// ***
} private static async Task<IInternalConfiguration> CreateConfiguration(IApplicationBuilder builder) { // make configuration from file system? // earlier user needed to add ocelot files in startup configuration stuff, asp.net will map it to this var fileConfig = builder.ApplicationServices.GetService<IOptionsMonitor<FileConfiguration>>(); // now create the config // *** fileConfig.OnChange(async (config) => { var newInternalConfig = await internalConfigCreator.Create(config); internalConfigRepo.AddOrReplace(newInternalConfig.Data); }); }
  Let's look IOptionsMonitor <T> is defined as follows:
  public interface IOptionsMonitor<out TOptions>
    {
        TOptions CurrentValue { get; }
        TOptions Get(string name);
        IDisposable OnChange(Action<TOptions, string> listener);
    }
IOptionsMonitor be seen from the following service registry is OptionsMonitor <>
  public static IServiceCollection AddOptions(this IServiceCollection services)
        {
// ***
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));return services; }

 

 

 

 

 

 

A physical file final disposal

      public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
         .ConfigureAppConfiguration((context, builder) => {
             builder.SetBasePath(context.HostingEnvironment.ContentRootPath)
             .AddJsonFile("Ocelot.json");
         })
         .UseUrls("http://localhost:6000")
         .UseStartup<Startup>()
         .Build();

Process webhost build the physical files are bound, as the AddJsonFile. AddJsonFile is defined as follows

public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange)

  IFileProvider achieved by, change notifications to the upper system.io achieve the ultimate physical file on the file system monitoring through.

  two 

 

Guess you like

Origin www.cnblogs.com/RunStone/p/9531159.html