I really like the new model, which is much more flexible than the old, static ConfigurationManager in full framework because it provides strong typing on configuration settings out of the box. In the past I'd been using my own Westwind.Utilities.Configuration setup from Westwind.Utilities that provided much of the same functionality (interchangeable providers) - with .NET Core there's no need for a third (or for me first) party solution as the in-the-box implementation provides most of those same features. Nice.

In the process of setting up a new application over the weekend I stumbled across an even simpler and - to me at least, cleaner - approach to configuring and injecting configuration into an application without using IOptions<T>.

Let's take a look.

Create your Configuration Object

ASP.NET Core's configuration system allows binding object properties to a series of providers. By default there's a JSON provider that looks at appsettings.json file, environment variables and the UserSecrets store. The config system can bind values from all these providers (and any others you might add) into a typed configuration object which can even include nested sub-objects.

I'm working on updating my blog to .NET Core - it's time: The blog is 12+ years old and still running Webforms. For that app the beginnings of my configuration object looks like this:

Note that you can easily nest the configuration objects which helps organizing complex configuration settings into easily segregated blocks. Here I separate out the email settings into a separate nested class.

I tend to use appsettings.json for most settings, and then use either user secrets for dev (so the values don't get shared to source control) or environment variables in production to feed in the sensitive values like passwords. Here's the relevant appsettings.json that has all the fields from my configuration mapped to a Weblog property key:

Setting up Injection in Startup.cs

To start, we need an IConfiguration instance which is the configuration root object. As of .NET Core 2.0 IConfiguration is a default service that can get injected automatically - it's one of the pre-configured services registered with the DI system as part of the .NET Core bootstrapping process.

The default .NET Core template now also provides IConfiguration in the Startup constructor:

This provides you a filled config object instance that has values set from the various configuration stores. I can take the configured object and add it to the DI provider as a singleton. From then on I can inject the raw Configuration instance into other components or views.

To inject this configuration singleton added above is now simply a matter of requesting the WeblogConfiguration in the constructor of any class that needs it:

I much prefer this over injecting IOptions<T> because it's more direct and specifies the actual dependency that's needed by the components, plus it's easier to set up tests that now don't have to get an instance of IOptions<T> from somewhere.

#PAGEBREAK

Compare to using IOptions<T>

Just so you know what I'm talking about when I say IOptions<T> implementation: Here's an example of how to set up the same behavior using IOptions<T> instead of the configuration singleton.

Obviously this isn't any more complicated, but it does require an extra layer of abstraction. IOptions<T> is just that - an abstraction wrapper without any real feature benefits. That may change in the future, but for now IOptions is just a wrapper around the configuration instance via the Value property.

IOptionsSnapshot can reload changed Config Settings

One advantage to using IOptions<T> or more specifically IOptionsSnapshot is that it can detect changes to the configuration source and reload configuration as the application is running.

For application level code this is perfectly fine, but if I do this for my repository which lives in a separate business project independent of the main application:

having to pass in IOptions makes the configuration a little less accessible for testing as you now need to provide IOptions rather than a plain POCO object.

IOptions provides additional features

After I posted this entry both Damien Edwards and David Fowler chimed in on Twitter that IOptions<T> provides a host of additional features that allow for composition through the lifetime of IOptions<T>.

In short there are potential benefits to using IOptions<T> over a raw configuration instance as the instance is essentially managed and additional services can be applied.

I'm having a hard time visualizing the use case David is talking about that I can't directly apply against a POCO instance that is passed through an application level configuration pipeline - if you can think of a good example or use case please chime in with a comment I would love to elaborate this point here.

Personally I've been using a more direct approach and haven't run into any issues with raw configuration instances, mainly because in the applications I tend to build the configuration is not all that dynamic. In fact I can't think of configuration that needs to be any more dynamic than the core features of the configuration system (via multiple providers) already handles.

Raw Configuration Value Binding

For completeness sake note that you can also bind string values by configuration path using the Configuration object indexer, which you can inject using the IConfiguration interface which in turn references a ConfigurationRoot instance. Using this object you get raw Configuration API access using string formatting where access a particular path key is path:key with 'paths' separated by : characters. You can think of this lower level API in the same way Configuration.AppSettings[] was used in full framework to provide string based configuration access, plus the benefits of multiple providers and nested configuration paths.

which can be sufficient if you have only a few configuration values in your app. Given how easy it is to create typed object mapped configuration though I find it hard to recommend this approach except for dynamic key access or startup code where the configuration objects may not have been set yet.

Static Instance?

As many of you know I'm not the biggest fan of DI for all things. I think there are a few things - and especially Configuration - that need to be easily and universally accessible in all situations. Especially in older versions of .NET Core it was painful to get at the configuration objects say inside of business or a system level component since configuration wasn't automatically injected by default. That's been fixed, but it can still be difficult to get access to the DI service context in some cases.

If you're building a generic component that needs to be reused in many different environments that you may not control, there's no guarantee that DI is available and configured. Configuration is a critical component that often is needed deep in the bowels of other components or necessarily static logic where it's not easy to get access to DI injected components. It never made sense to me to force DI on simple few-line helpers when a static function that have no other dependencies.

Long story short: It is sometimes useful to be able to get at a static instance of configuration and while I try to avoid introducing singleton statics like this in most cases, I think configuration is one of those cases where it makes sense (at least to me).

So, I decided to also add a static property that holds the Current instance:

Since the configuration object is a singleton anyway, the Current property is implicitly set only once when the code in ConfigureServices() initially binds the config instance and it points at the same object instance that also gets injected. After that you can then use either DI whenever possible - ie. most cases - and the static property in those few special cases when it's difficult to get access to the DI context.

In my Weblog for example, I'm copying over a lot of old helper code from the old Weblog application and there are static function helpers that generate a bunch of small HTML bits like this:

The static property makes this code work easily without having to refactor all of the ported functions in this class to a (pointless) class wrapper.

This gives me the best of both worlds: I get the ability to inject configuration where I can get at the DI context (just about all new code), and use the static in the few cases when DI is not easily available (legacy code).

Many Options - Choice is good

In ASP.NET Core there are many ways to handle configuration and that's a good thing. As I mentioned I ditched my own custom solution in favor of the new configuration system in .NET Core which is good enough out of the box. The only thing I'm really missing is an easy way to update configuration stores and force initial values to be written, but overall that's a minor concern and doesn't make sense for all the supported providers (how do you update Environment variables loaded from a startup script for example?)

The good news is that the configuration engine is very flexible and provides a number of different ways to get at configuration objects.

Raw Configuration[path:key]

IOptions<T> binding to a Config Section

Configuration.Bind() to bind to a Config Section

Static Props if you want it

Choice is good - for now I'm choosing the instance based approach because to me at least it seems it's the most intuitive for working with configuration. Your mileage may vary...

The Voices of Reason

Rick, in your previous article on ASP.net Core configuration, as well as your Album Viewer code, the ConfigurationBuilder() was used to provide the location of appsettings.json or specifying a dev or prod configuration. I don't see any mention of the ConfigurationBuilder() class in this post and I am wondering how/if it still applies.

@Mike - ConfigurationBuilder is now implicitly used by ASP.NET to initialize the configuration system, so you don't have to call it explicitly, unless you have a specific need to get at settings before they are initialized. The code in AlbumViewer probably does this because it predates .NET Core 2.0 where the config system was not yet initialized in Configure Services (and any middleware bits you accessed).

By best of both worlds I mean this would allow you to inject your AppSettings object directly into services, yet still have just-in-time binding and all the other benefits of using IOptions that David Fowler mentioned.

To confirm this works as expected, I use IOptionsSnapshot above instead of IOptions and verified that changes to appsettings.json are indeed picked up without restarting the app.

This and your previous post on configuration inspired me to write 3 posts on my blog. The latest is at https://stevetalkscode.co.uk/configuration-bridging-part-3 which takes Todd's idea of using a lambda a step further by using a bridging class which allows injection of other classes into the settings class for functionality such as settings validation and decryption.

Thank you for the write up on binding configuration to an object. I was handling parsing IConfiguration manually until my app began to grow and become more complex. Having been aware of binding, I came looking specifically for an example of how to do so and found your page. I was hoping to see an example of binding a collection property in my config object with an array in my config file. I will try it now and see what happens. Thanks for your example to get me to the point I need to try this!

if you can think of a good example or use case please chime in with a comment I would love to elaborate this point here.

Reading David's tweet above, I believe, when you are building a modular application in ASP.NET Core, where multiple modules need to "inject" something that needs to be retrieved later by a certain "Core" module (assuming you need to collect settings from all modules in the app). Then, by using services.Configure() multiple times (each module will be using it), then this way, the ASP.NET Core Framework would collect all instances from all modules and make them available for you.

For instance, you might define a Dictionary read-only property inside a custom Options class and then each module would read that dictionary and append to it some settings or whatever the business logic you need.

The end result is that the custom Options class having its read-only dictionary property, collecting all values that were added by all modules.

I can think of two use cases besides the module one. The main use case I see is for options that are computed properties.

Recently I built a service that used a probabilistic data structure. The optimal size for that structure could be determined only by experimentation, but a pretty good guess could be computed from the application's configuration.

Using IOptions I was able to set a default data structure size property based on the other configuration settings. If the user provided a value in the configuration then of course I just used that.

You could do this with a custom configuration provider, or with an application specific configuration object. But IOptions made this simple and uniform.

The additional benefits come from lifetime management. A typical occurrence for us is the adjustment of configuration settings when dealing with unexpected load or resource failures. The snapshot and change monitoring abstractions provide a well known pattern for doing this. A snapshot means that in flight operations have a consistent set of configuration values no matter when they're read; and similarly the configuration monitoring provides a current view of values no matter when the configuration changes. Those things become very important at scale where these sorts of changes need to occur without interrupting the service or having to take resources offline while you cycle their configuration.

Again, you could do that with an application specific layer; but why?

When you use an application specific layer you have to teach the rest of your team how it works and how to use it. With options, you just send them to the docs. I'm much happier if they RTFM than have me walk them through my code.

My point is that there's a lot of value in just using consistent and common design patterns. Especially if it means you don't have to create your own string class (I mean configuration management)

Thanks Rick.
Without knowing what the 'prescribed' ASP.Net Core way was, the way I imagined configuration would work for DI is pretty much exactly how you showed it in this article.
I appreciate the effort you've gone to documenting it! (a year or more ago)

This works great, except for when you have an array of objects. Let's say you have an array of email called Emails. If you do it as a list of email, it comes up null. If you create a poco for Emails, it will have no values. I can't figure out how to get this to bind.

I have this working for all of my app settings json files, but what about other configuration settings? For example we use Hashicorp vault to store secret settings, then add those as a Dictionary<string,string> as an in memory collection (Iconfigurationbuilder.AddInMemoryCollection).