SparkyTools: XmlConfig

part of the “SparkyTools” suite of .NET utility libraries

photo of jigsaw puzzle by Chad Valley from Birmingham Museum and Art Gallery
NuGet Package | Source Code

If you want to load configuration data for .NET application from a web.config or app.config section other than the build-in sections (e.g. appSettings, connectionStrings), you have to write a custom IConfigurationSectionHandler implementation to handle your custom .config section. It’s not hard to do, but it’s a fairly tedious coding exercise. What if you didn’t have to?…

ConfigurationSectionDeserializer / ConfigurationSectionListDeserializer

These classes makes it easy to load a strongly-typed object (or IList of objects) from a custom web.config or app.config file section without having to write a custom IConfigurationSectionHandler implementation.

In the .config file, register each custom section with a type of “SparkyTools.XmlConfig.ConfigurationSectionDeserializer” or

    <section name="Foo" type="SparkyTools.XmlConfig.ConfigurationSectionDeserializer, SparkyTools.XmlConfig" />
    <section name="FooList" type="SparkyTools.XmlConfig.ConfigurationSectionListDeserializer, SparkyTools.XmlConfig" />

In each registered custom section, specify the object type via the type attribute. Here's an single instance section:

<Foo type="FooNamespace.Foo, FooAssemblyName">

…and a “list” section: (the type is the instance type, not "IList..."):

<FooList type="FooNamespace.Bar, FooAssemblyName">

To read from your custom .config section, just call the Load method, specifying the object type and the .config section name:

Foo foo = 
IList<Foo> fooList = 

DependencyProvider methods

These methods create DependencyProviders (using the SparkyTools.DependencyProvider NuGet package) for use with apps that use app.config / web.config files:

  • ConfigurationSectionDeserializer.DependencyProvider / ConfigurationSectionListDeserializer.DependencyProvider
    create DependencyProviders that load data from a .config file section.

Class using DependencyProviders:

using SparkyTools.DependencyProvider;

public class Foo
public Foo(
DependencyProvider<Bar> barProvider,
DependencyProvider<IList<Baz>> bazProvider)

Instantiating the class using DependencyProviders that load from .config:

using SparkyTools.XmlConfig;
. . .
var foo = new Foo(

AppSettings.DependencyProvider creates a DependencyProvider that wraps ConfigurationManager.AppSettings.

Class using appSettings DependencyProvider:

using SparkyTools.DependencyProvider;

public class Qux
private readonly Func<string, string> _getAppSetting;

public Qux(
DependencyProvider<Func<string, string> appSettingsProvider)
_getAppSetting = appSettingsProvider.GetValue();

public void MethodUsingAppSettings()
string valueFromAppSettings = _getAppSetting("key);

Instantiating the class using a DependencyProvider using the .config file appSettings section:

using SparkyTools.XmlConfig;
var qux = new Qux(AppSettings.DependencyProvider());