Validating Configurations in an ASP.NET Core 2.1 Application Using Data Annotations

Cheranga Hatangala
Cheranga
Published in
4 min readJan 25, 2019

Earlier, I wrote an article about how to validate your strongly typed configurations as early as possible. This article is an extended approach of it using data annotations and using an implementation of “IStartupFilter”. The filter was created based on Andrew Locke’s great article which can be found in here. I have created a nuget package for this and you can use it by searching for “ValidateConfig” in nuget package manager. The source code is available in GitHub.

The Configurations

Let’s add a couple of configuration classes which we think will be used in the application.

  • NotificationServiceConfig (if the application is using some notification service)
  • DatabaseConfig (if the application want to use a database connectivity)

There are two things to notice here. Both of the configuration classes are implementing an interface called “IValidateConfig” and the properties which needs to be validated are decorated with data annotation attributes which can be found in “System.ComponentModel.DataAnnotations”. The “IValidateConfig” is a marker interface which will be used to register the configurations in the DI and the attributes will help to validate the class when the startup filter checks whether the configuration is valid. You can see how it’s done below in the “ValidateConfigurationFilter” class.

The “ValidateConfigurationFilter” — Startup Filter

This is the class which implements the “Microsoft.AspNetCore.Hosting.IStartupFilter” interface.

As shown in the above image,

  • It implements the “IStartupFilter” interface.
  • The class requests an “IEnumerable<IValidateConfig>” from the IoC container (will show how these are registered below).
  • Inside the “Configure” method it checks whether there are any validation errors by traversing this collection.
  • If there is an invalid configuration an exception of type “InvalidConfigurationException” is thrown.

Registering the dependencies

Let’s register the configurations and the startup filter as dependencies now. For this we’ll add a method called “RegisterDependencies” in the “Startup.cs” class and, we’ll call it from the “Configure” method.

As you can see the configurations are registered as an “IValidateConfig”.

Testing with Invalid Configurations

Let’s add the relevant sections in the “appconfig.json” file.

Hit F5 or Ctrl+F5 and, as you can see below, the API does not load and shows an error.

Testing with Valid Configurations

Because every bad thing has a good thing, let’s check with some valid settings.

Now let’s start the application.

Great! Because the configuration is valid, the application loads successfully.

“ValidateConfig” Nuget Package

We have to write more than a few lines of code to register the configurations and the startup filters in the “Startup” class. What if we have a nuget package, which does all this for you and, you just need to install it and use it’s extension methods?

I have created a nuget package called “ValidateConfig” for this purpose. This is how you use it.

  • Install the nuget package

Install-Package ValidateConfig

  • In your startup class, in the “Configure” method add the following code.

Use the “RegisterConfig” extension method to register your strongly typed configurations and, once you are done call “ValidateConfigs” so that it will register the start up filter with the DI.

Thanks, let me know your comments, critics.

--

--