ASP.NET Core managed config

By | 2019-06-20

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

Your email address will not be published. Required fields are marked *