Adding Custom Configuration Sources

Configuration are a key part of any application with them it would be clueless on what to do.
They are retrieved from many different sources such as web.config, appsettings.json, env variables and so many others.
All of those source serve the same purpose of providing a key value dictionary that is not changed much but configured in the applicaiton.
Lucky! in .Net we have the IConfiguration interface which is dependency injected in the places that we want by the framework it self, after which we can use it as a dictionary to retrieve key values.
execute the following command on your NPM
install Microsoft.Extensions.Configuration.Abstractions
we need to know how is the IConfiguration instance gets created.
If we look closer we could see the builder which is IConfigurationBuilder, it is the one responsible for getting and creating an instance of IConfiguration.
//
// Summary:
// Represents a type used to build application configuration.
public interface IConfigurationBuilder
{
....
//
// Summary:
// Gets the sources used to obtain configuration values
IList<IConfigurationSource> Sources { get; }
....
}
As we see there is a list of IConfigurationSource, each source is responsible of creating its own provider which must of type IConfigurationProvider.
So to recall we have a builder that holds configuration sources and each source creates its own provider on which the provider simply holds a dictionary which it populates based on its needs.
By understanding this we now can add our custom sources which would be added to the IConfiguration injected instance so we could easily use it among the application with out adding any extra services.
Lets dig deeper in the code, I have created and empty asp.net core 2.1 web-app.
Playing with the configuration is going to be at the root level of our application so we need to change in program.cs.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, buider) =>
{
buider.Add(new MyCustomConfigurationSource());
})
.UseStartup<Startup>();
}
Not much changed I called the extension method ConfigureAppConfiguration which would expect an action that it would pass the HostingContext that gives information about our environment and an instance of IConfigurationBuilder which holds an Add function for new sources.
My source looks like the following.
public class MyCustomConfigurationSource : IConfigurationSource
{
public IConfigurationProvider Build(IConfigurationBuilder builder)
{
new MyCustomConfigurationProvider();
}
}
I need to implement from IConfigurationSource which has Build method that gets internally called in the builder.
As we see I am returning a custom provider.
public class MyCustomConfigurationProvider : ConfigurationProvider
{
public override void Load()
{
Data.Add("TestKey", "Value");
}
}
In here I have inherited from ConfigurationProvider which is an abstract base class and then overridden Load method.
The base class has a Data dictionary which we need to set key values in.
And that is it!, we now can get our custom configuration straight away from IConfiguration instance.
Lets see it in action.

As we see this is the default sources that we get which great on there own!
And now lets see what happens when following previous steps.

WOW, we got our source to be added and we can simply access like any other key!
This could be used in many cases such as setting some configuration in database and then managing them there.
Hope you enjoyed the read!.
No Comments