Configuration Management in Legacy .NET Applications (.NET Framework 4.5.1+)

Most of the web-based .NET applications are using appSettings section of the web.config or/and a static class (usually called ApplicationConfiguration or similar) to store configurations. In rare cases, you can find that someone is using custom web.config sections and map them into the code, which is honestly not the most pleasant thing to do. Through the time amount of configuration keys is growing and web.config files are blowing up to unreadable sizes. Maintainability of the configuration becomes a hard job which nobody want to do.

As soon as you are doing CI/CD and building for different environments (staging/production, etc.) you also start to get the pain of changing configs during the build time. Sure, you can use web.config transformations approach or similar solutions. But more environments you will have — more transforms will appear in the project folder.

What about security? We often store sensitive information in our code and config files. Just try to search that kind of information in code and web.configs of any legacy application, I bet you will find half a dozen of production tokens, hardcoded logins, passwords, and secret URLs.

In .NET world people were struggling and having problems with configuration for a long time. Web.config and hardcoded values — definitely not the way how configuration management should be done!

Web.config is obsolete. Is there a better solution?

As we can see, there is support for loading JSON files, environment variables, and others out of the box. There is also a number of other useful features, like:

  • Reloading configuration without application restart
  • Configuration snapshots (versions)
  • Easy way of adding custom data providers
  • Safe way of working with encrypted data and data stores like Azure Key Vault

With new API there is no need to store the whole configuration in one big static class. With new Options Framework you can map different parts of the configuration tree to separate objects:

Or even complex object graphs:

Usage is really simple: just inject IOptions<T>(where T is your Option class) into class by using favourite IoC container:

That is really awesome, but we are not running .NET Core!

.NET Core is a shiny future

Using ASP.NET Core Configuration API and Options in .NET

  1. Install and use .NET Core Dependency Injection Abstraction.
  2. Reimplement extension methods, using our current IoC Container API.

The first option is doable but would cause a lot of issues, collisions, and conflicts between existing and new DI mechanisms.
So let’s take a look at the second way as it is easier to use in the legacy system.

First of all, we need to reimplement registration of Options Framework with using our DI (in examples we will use Autofac):

Then we will need two methods that allow us to register Option mapping classes. First is the way of registration:

And second — takes delegate as an argument:

Now we are able to register our Option mappings in the beginning of the DI setup:

Each injection of IOptions<MailingOptions> or IOptions<FeedOptions> will be resolved as an instance of a proxy class that contains mapped data.

One of the great features of Options framework is that we have hot reloading of the configuration without restarting the application pool (as you remember, each time when you change web.config, the application pool is restated). To use reloadable configuration we need to add reloadOnChange parameter to AddJsonFile method and inject IOptionsSnapshot<T> instead of IOptions<T>. Now on any change of JSON file, Options Framework will inject the latest version of the configuration. Also, you can mix injections of IOptionsSnapshot<T> and IOptions<T> to allow only partial hot reloading.

There are many other features in the new .NET platform. You can learn more about them here.


Don’t miss the possibility to use brand new features in old applications, that can help you to learn and use fresh technologies even in legacy systems.

Here is a link to the demo project, that showcase usage of Options Framework in .NET 4.5.1 and newer.

Interested in JavaScript, Node.js, .NET Core, Docker. Author of @StatsShow. Co-organizer of @netCoreBCN and ReactJS Barcelona