Reputation: 5374
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
Reputation: 11544
There are several ways to automate validating strongly typed configuration but for key/value config["property1"]
I don't think so.
Adding validation to strongly typed configuration objects in ASP.NET Core
Validating Configuration objects in ASP.NET Core
Validate strongly typed options when using config sections
Using ASP.NET Core configuration mechanism with valid objects
Upvotes: 1
Reputation: 1026
You can use Options
they have the possibility to add validations rules and much more like
to update on some changes etc..
Upvotes: 2