Allow optional integration of configuration providers#894
Allow optional integration of configuration providers#894jsonBackup wants to merge 1 commit intoPHOENIXCONTACT:futurefrom
Conversation
There was a problem hiding this comment.
First draft looks promising to me 👍 The following point you could consider next:
- Add/Modify documentation accordingly
- Add a short tutorial on how to populate config values e.g. in the MQTT or OPC UA Driver config from environment variables
- Add code or a seperate ticket for nested properties in objects in the config
- Clean up some of the code, test names, comments, etc.
I think the limitation to string properties could only be overcome with the implicit config population and since you only consider password properties there they will need to be of type string to 😅 But I think you could split this up into a separate issue, if you already have an idea on how to solve this, keeping this change limited to string properties would be alright for me.
| public ModuleController(IModuleContainerFactory containerFactory, IConfigManager configManager, ILoggerFactory loggerFactory, IDbContextManager contextManager) | ||
| : base(containerFactory, configManager, loggerFactory) | ||
| { | ||
| // first tests: |
There was a problem hiding this comment.
Nice that this works, but I think it should not be the focus of this pr 🙂
There was a problem hiding this comment.
Remember to remove before merge
|
|
||
| // NEW: hook up IConfiguration from the host | ||
| var configuration = serviceProvider.GetService<IConfiguration>(); | ||
| if (configuration != null) |
There was a problem hiding this comment.
As the property is nullable no need for the extra check, is it?
There was a problem hiding this comment.
isn't the property filled by the service collection?!
| config = CreateConfig(confType, ConfigState.Generated, "Config file not found! Running on default values."); | ||
|
|
||
| // Defaults & shared configs | ||
| ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddProviders(ValueProviders)); |
There was a problem hiding this comment.
Executes the value provider (e.g- default values...) I wonder why this was not applied before?
| _sharedProvider = new SharedConfigProvider(this); | ||
| } | ||
|
|
||
| protected virtual void ApplyConfigurationProviders(object config) |
There was a problem hiding this comment.
Can't you already limit this to ConfigBase?
| if (string.IsNullOrWhiteSpace(candidateKey)) | ||
| continue; | ||
|
|
||
| var providerValue = Configuration[candidateKey!]; |
There was a problem hiding this comment.
What happens here if multiple providers are available?
| if (!string.IsNullOrEmpty(providerValue)) | ||
| { | ||
| // Replace placeholder with the actual secret | ||
| prop.SetValue(config, providerValue); |
There was a problem hiding this comment.
Add logging that a variable was used from a conifguration provider, if available from which configuration provider. But do NOT log the variable value
| if (!string.IsNullOrWhiteSpace(currentValue) && currentValue!.Contains(":")) | ||
| { | ||
| // Config file holds the key directly | ||
| candidateKey = currentValue; | ||
| } |
There was a problem hiding this comment.
This condition is not enough -do we have other indicators?
|
|
||
| ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddProviders(ValueProviders)); | ||
|
|
||
| // NEW: overlay values from Microsoft.Extensions.Configuration providers |
| // Defaults & shared configs | ||
| ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddProviders(ValueProviders)); | ||
|
|
||
| // NEW: even here we can try to fill from providers |
| config = CreateConfig(confType, ConfigState.Generated, "Config file not found! Running on default values."); | ||
|
|
||
| // Defaults & shared configs | ||
| ValueProviderExecutor.Execute(config, new ValueProviderExecutorSettings().AddProviders(ValueProviders)); |
There was a problem hiding this comment.
Executes the value provider (e.g- default values...) I wonder why this was not applied before?
|
|
||
| // NEW: hook up IConfiguration from the host | ||
| var configuration = serviceProvider.GetService<IConfiguration>(); | ||
| if (configuration != null) |
There was a problem hiding this comment.
isn't the property filled by the service collection?!
| private ConfigManager CreateManager(IConfiguration configuration) | ||
| { | ||
| return new ConfigManager | ||
| { | ||
| ConfigDirectory = _tempDir, | ||
| Configuration = configuration | ||
| }; | ||
| } |
There was a problem hiding this comment.
Private methods to the bottom of the class -> under the tests
| // NEW: hook into Microsoft.Extensions.Configuration | ||
| public IConfiguration? Configuration { get; set; } |
There was a problem hiding this comment.
Remove this info "NEW"..., add a well descriptive Summary
#710