0

Imagine the following:

public class FooOptions { 
  public string FooProperty {get; set;}
}

public class BarOptions { 
  public string BarProperty {get; set;}
}

public class ConfigureBarOptions : IConfigureOptions<BarOptions> {
  private readonly IOptionsMonitor<FooOptions> _fooOptionsMonitor;

  public ConfigureBarOptions(IOptionsMonitor<FooOptions> fooOptionsMonitor) {
    _fooOptionsMonitor = fooOptionsMonitor;
  }

  public void Configure(BarOptions options) {
    options.BarProperty = _fooOptionsMonitor.CurrentValue.FooProperty;
  }
}

// in Startup.cs - bind to a configuration provider that is set to reload
services.Configure<FooOptions>(Configuration);

How can I trigger a reload of BarOptions when FooOptions is reloaded due to an underlying configuration update?

I've played around with various attempts at registering some versions of:

services.AddSingleton<IOptionsChangeTokenSource<BarOptions>>(new ConfigurationChangeTokenSource<BarOptions>(name, config));

I've also tried to think of a way to use ChangeToken or _fooOptionsMonitor.OnChange but haven't seemed to get the right implementation.

user3119533
  • 186
  • 1
  • 7
  • What is the actual problem you are trying to solve, obviously if bar hasn't changed there is no options to change, and the onchange wont fire, but there is nothing stopping you from taking a dependency on both. or if you need to then create a service for it. or if you really feel like burning your time, create your own custom configuration source and provider. – TheGeneral Oct 28 '21 at 06:05
  • @TheGeneral, in my web api I need to configure both the JwtBearerOptions and the SwaggerGenOptions with the same OpenIDConnectUrl. The JwtBearerOptions is bound to the config source and is injected into my ConfigureSwaggerGenOptions class. I want to re-execute the ConfigureSwaggerGenOptions class when the JwtBearerOptions changes so it can get the new URL. I did try binding both options classes directly to the config source but the swashbuckle library seems to be written in a way that breaks config reloads. Also, I already have a custom config source if that somehow helps. – user3119533 Oct 28 '21 at 14:37
  • This might actually be a swashbuckle issue unfortunately., I cant really suggest much or help from here. Maybe you can take this question to github and ask for clarification Note if you do get as suitable answer, you can reword this question to directly relate to it, and write up your own answer which explain the results of the clarification – TheGeneral Oct 28 '21 at 21:36
  • I actually already posted it as an [issue](https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2262). But I was really hoping for a generic way to trigger configuration actions to fire again. That would give me a workaround for this issue but also be generally useful. – user3119533 Oct 29 '21 at 01:05

0 Answers0