-
Notifications
You must be signed in to change notification settings - Fork 0
Passing Arguments to Configuration Source
Configuration Adapter is a "bridge" between configuration engine and configuration source. This bridge is represented by class implementing IAnyWhereConfigurationAdapter interface:
public interface IAnyWhereConfigurationAdapter
{
void ConfigureAppConfiguration(
IConfigurationBuilder configurationBuilder,
IAnyWhereConfigurationEnvironmentReader environmentReader);
}Configuration Adapter doesn't produce any application configuration. Instead of this it adds additional configuration sources into application configuration:
public class SomeConfigurationSource : IConfigurationSource
{
// ... implementation
}
// ... code
configurationBuilder.Add(new SomeConfigurationSource());In cases when implementation of IConfigurationSource requires input arguments we need to pass these arguments to configuration adapter.
This can be done by specifying LOCAL variables or by using in Configuration File.
To better understand how this works consider the following example where MySource configuration source requires a string argument to be passed in:
public class MySourceAdapter : IAnyWhereConfigurationAdapter
{
public class MySource : IConfigurationSource
{
public MySource(string argument)
{
// ...
}
}
}
// ...
configurationBuilder.Add(new MySource(/* ??? */));LOCAL variables are consumed by both configuration engine and configuration adapters. They are specified using special environment variables of the following format: ANYWHERE_ADAPTER_{INDEX}_{VARIABLE_NAME}:
- ANYWHERE_ADAPTER - is a predefined prefix.
- {INDEX} - is a zero based index of the adapter being configured.
- {VARIABLE_NAME} - is a name of the variable.
In the example above the argument can be passed using ANYWHERE_ADAPTER_0_ARGUMENT=<argument value> environment variable.
Then variable value is consumed inside MySourceAdapter by using provided instance of IAnyWhereConfigurationEnvironmentReader:
var argument = environmentReader.GetValue("ARGUMENT", optional: false);
// ...
configurationBuilder.Add(new MySource(argument));NOTE
When LOCAL variables are consumed inside configuration adapter the ANYWHERE_ADAPTER prefix and INDEX aren't specified.
Configuration File is represented by a physical file with the same name as configuration adapter assembly but with .anywhere extension. The file and assembly should be collocated.
Content of configuration file is a Environment.NewLine separated list Key=Value pairs where each pair represents a variable with it's value.
In the example above MySourceAdapterAssembly.anywhere would have the following content:
ARGUMENT=<argument value>
NOTE
Variables specified in configuration file doesn't have ANYWHERE_ADAPTER prefix and INDEX.
Then variable value is consumed inside MySourceAdapter by using provided instance of IAnyWhereConfigurationEnvironmentReader:
var argument = environmentReader.GetValue("ARGUMENT", optional: false);
// ...
configurationBuilder.Add(new MySource(argument));(c) 2020 Coherent Solutions Inc.
GENERAL
GETTING STARTED
ADAPTERS
SAMPLES