Using Other Providers with AppConfiguration
By default the AppConfiguration class uses the .config files for configuration storage. You can easily choose one of the other providers however or create your own to store in a different configuration store. You can also override the default behavior to customize the provider's operation.
There are two ways that this can be accomplished:
- Pass a customize Provider to Initialize()
- Override the OnCreateDefaultProvider() on your Config Class
Pass a customized Provider to Initialize()
An easy way to specify and customize a provider is to simply create it in your startup code, customize the provider and then pass it to the Initialize() method:
var provider = new ConfigurationFileConfigurationProvider<CustomConfig>()
{
//ConfigurationFile = "CustomConfiguration.config",
ConfigurationSection = "MyApplicationConfiguration",
EncryptionKey = "ultra-seekrit", // use a generated value here
PropertiesToEncrypt = "Password,AppConnectionString"
};
App.Config = new CustomConfig();
App.Config.Initialize(provider);
This works great and if you use the provider only in a single application this is perfectly sufficient.
Add OnCreateDefaultProvider() Method to your Configuration Class
Typically you only need one way to access configuration information, so it's actually a better idea to encapsulate the default provider configuration as part of your custom class.
To create custom configurations the AppConfiguration class includes two override points: OnInitialize() and OnCreateDefaultProvider(). The latter zeros in only on provider creation while the former needs to handle both provider creation and initial loading of the configuration values (typically via the Read() method).
In most cases overriding OnCreateDefaultProvider() is sufficient. Here's an example implementation:
public class CustomConfigConfiguration : AppConfiguration
{
public string ApplicationName { get; set; }
public DebugModes DebugMode { get; set; }
public int MaxDisplayListItems { get; set; }
public bool SendAdminEmailConfirmations { get; set; }
public string Password { get; set; }
public string AppConnectionString { get; set; }
public CustomConfigFileConfiguration()
{
ApplicationName = "Configuration Tests";
DebugMode = DebugModes.Default;
MaxDisplayListItems = 15;
SendAdminEmailConfirmations = false;
Password = "seekrit";
AppConnectionString = "server=.;database=hosers;uid=bozo;pwd=seekrit;";
}
protected override IConfigurationProvider OnCreateDefaultProvider(string sectionName, object configData)
{
var provider = new ConfigurationFileConfigurationProvider<CustomConfigConfiguration>()
{
//ConfigurationFile = "CustomConfiguration.config",
ConfigurationSection = sectionName,
EncryptionKey = "ultra-seekrit", // use a generated value here
PropertiesToEncrypt = "Password,AppConnectionString"
};
return provider;
}
}
The key is the overridden OnCreateDefaultProvider() method which creates the provider and returns it. The method receives two optional parameters which are forwarded from the Initialize() method. The sectionname will be set to a default value or the value the user provided.
The configData parameter is optional and allows passing custom information from the Initialize() method down to the provider. This is useful if there are additional requirements for initialization - for example, you could pass down the name of a configuration to store the data into in configData.
The nice thing about this approach is that it encapsulates the provider configuration in the class, so if you use the configuration class in multiple projects you don't have to duplication the provider configuration as part of your application code anywhere.
This method is used only if no provider is passed to Initialize(), so even if implemented you can still override the provider by passing a custom provider.
© West Wind Technologies, 1996-2023 • Updated: 12/19/15
Comment or report problem with topic