mu88
mu88

Reputation: 5374

Configuration checks in .NET Core

With .NET Core, reading application configuration via Microsoft.Extensions.Configuration.IConfiguration has changed significantly. I know that there are a couple of packages and blog posts about how to read different configuration sources: environment variables, JSON files, command line, etc. But until now, the topic of validating the configuration hasn't been covered so much - at least it didn't reach me.

Does anybody know some NuGet package (the more official the better) that allows to specify certain rules/checks based on the configuration? For example, at the moment, my application startup usually looks like this:

var config = new ConfigurationBuilder()
                 .AddJsonFile("appsettings.json", true)    
                 .AddEnvironmentVariables()
                 .Build();

if (string.IsNullOrEmpty(config["property1"])) throw new ArgumentNullException("property1");
if (string.IsNullOrEmpty(config["property2"])) throw new ArgumentNullException("property2");

Do I have to write my own extension methods for IConfiguration or is there already a "ready to use" package?

Upvotes: 0

Views: 544

Answers (2)

Support Ukraine
Support Ukraine

Reputation: 1026

You can use Options they have the possibility to add validations rules and much more like to update on some changes etc..

Option validation

Upvotes: 2

Related Questions