ASP.NET Core managed config

Reading config is pretty straight forward task that most applications need. For many years I used Nini, which scratched that itch fairly well. With the advent of better libraries its not too difficult to deserialize your favorite formatted config file into a class.

With .Net Core we finally got some decent/easy to use config system built in by the help of the Options pattern. In short, reading config by deserializing sections of appsettings.json into DI-hosted objects.

Install

Implement

In Startup.cs ConfigureServices method:

This would require a couple of classes, for example:

And a corresponding config section

If config section/values do not exist it will not produce any error.

Using

Recall config object by IOptions<object> and use its “Value” property to extract object.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Tedds blog

Subscribe now to keep reading and get access to the full archive.

Continue reading