Repository navigation
Description
Updated by @maryamariyan:
Goal
When an application starts, we want to get immediate feedback on validation problems. e.g. we would like to get exceptions thrown on app startup rather than later.
Benefits of eager validation:
- Enabling this feature forces the program to crash fast when an invalid configuration is passed, as opposed to the default lazy validation only when
IOptions<T>is requested
API Proposal
ValidateOnStart feature is implemented as extension to OptionsBuilder<TOptions>
To allow for eager validation, an API suggestion is to add an extension method on OptionsBuilder, (not IHost).
Usage:
services.AddOptions<MyOptions>()
.ValidateDataAnnotations()
.ValidateOnStart(); // Support eager validationAPIs:
According to usage, we'd need the APIs below:
namespace Microsoft.Extensions.DependencyInjection
{
public static class OptionsBuilderExtensions
{
public static OptionsBuilder<TOptions> ValidateOnStart<TOptions>(this OptionsBuilder<TOptions> optionsBuilder) where TOptions : class;
}
}Focus of this issue:
The focus here is on eager validation at application startup, and these APIs don't trigger for IOptionsSnapshot and IOptionsMonitor, where values may get recomputed on every request or upon configuration reload after the startup has completed.
- It would support named options too.
IOptions<TOptions>:
- Reads configuration data only after the app has started.
- Does not support named options
IOptionsSnapshot<TOptions>:
May be recomputed on every request, and supports named options
IOptionsMonitor<TOptions>:
Is registered as a singleton, supports named options, change notifications, configuration reloads, and can be injected to any service lifetime.
Original Description (click to view)
AB#1244419
From exp review with @ajcvickers @DamianEdwards @Eilon @davidfowl
We should support some mechanism for eager (fail fast on startup) validation of options.
Needs to also work for generic host as well as webhost, must be configurable on a per options instance, since this will never work for request based options.
Activity
hishamco commentedon Aug 5, 2018
I think @andrewlock did something similar for Configuration. Andrew could you please share some of your thoughts here
andrewlock commentedon Aug 5, 2018
I wrote a post about my approach here: https://andrewlock.net/adding-validation-to-strongly-typed-configuration-objects-in-asp-net-core/, and I have NuGet package to provide the funcitonality: https://github.com/andrewlock/NetEscapades.Configuration/blob/master/src/NetEscapades.Configuration.Validation
Essentially, I implement a simple interface in my options objects (
IValidatable), and register it with the DI container. You can perform the validation any way you like (DataAnnotations, Fluent validation etc).Then I have a
StartupFilterthat just callsValidateon all the settings:Any exceptions will happen on app startup. There's a few caveats to this approach:
IStartupFilteris HTTP/middleware specific AFAIK, not generic hostIOptionsMonitorapproach.HaoK commentedon Aug 5, 2018
Thanks @andrewlock I'm guessing we will end up with something similar just in a more generic host friendly way, that probably uses options itself to configure what option instances to validate at startup.
HaoK commentedon Aug 27, 2018
Out of time to get this in this week for preview2, will start a PR this week targeting preview3
HaoK commentedon Aug 30, 2018
Discussed with @ajcvickers current thinking will be to add the add a way to register startup actions which happen to do options validation.
Thoughts @davidfowl ?
HaoK commentedon Sep 17, 2018
Per review with @DamianEdwards @davidfowl @ajcvickers @Eilon we are going to ship 2.2 without eager validation
The existing eager validation code in the PR will be moved into the tests and referenced/explained in the 2.2 samples/documentation so we can gather feedback before adding official support
JechoJekov commentedon Jan 20, 2019
I have created a small class (based on the idea by @andrewlock of using an
IStartupFilter) that "extends" theOptionsConfigurationServiceCollectionExtensions.Configuremethod by adding validation and also supports eager validation at startup.Usage:
Class:
JechoJekov commentedon Jan 20, 2019
The empty error message of
OptionsValidationExceptionis utterly unhelpful - at least an error message containing the number of errors and the first error would be much more helpful. As the current implementation stands unless the exception is specifically caught and the containing errors manually logged there is no way to know what the error is except that it is somewhere in the configuration. Even having the first error message will be extremely helpful as even if there are more than one errors they can be addressed one by one.A possible implementation:
HaoK commentedon Apr 17, 2019
No plans to add eager validation yet
michaelgregson commentedon Sep 30, 2019
Thank you for all the great ideas in this thread. I wanted to retain the Validate and ValidateDataAnnotations provided by OptionsBuilder but enable eager validation, the patterns above led me to:
Usage:
Classes:
wdolek commentedon Oct 3, 2019
Just be aware that using
IStartupFilterwon't work for non-host apps, consider command line application, Azure Function or AWS Lambda. (I would expect something more handy than manually resolving options from service provider)Another thing to consider: how should be "reloadOnChange" handled? Last thing I want is to throw at runtime. With current instrumentation of
IOptionsMonitor<T>.OnChangeI get last valid options (good), in other hand, I'm not getting notification about changed invalid options (not so good).ArgTang commentedon Oct 4, 2019
For container loads, that genericHost is perfect for, changing of config at runtime would not be a common usecase.
24 remaining items
terrajobst commentedon Jan 28, 2021
Video
ValidateAnnotationsEagerlyshould beValidateDataAnnotationsEagerlyto be a suffix of the existing APIsValidateEagerlyandValidateDataAnnotationsEagerly?ValidateEagerlyfor nowEagerlythe right term here? I think we'd preferOnStartupAny opinions @davidfowl?davidfowl commentedon Jan 28, 2021
I prefer ValidateOnStartup, though people may think it has something to do with the Startup class. What about
ValidateOnStart?maryamariyan commentedon Jan 28, 2021
The ValidateDataAnnotationsOnStartup would validate data annotations,
would be same as calling (shorthand for):
but it is also possible to validate options that are specified using a delegate without data annotations, e.g.
Doesn't seem like we'd need the shorthand for DataAnnotations, so the plan is to just add
ValidateOnStartup(orValidateOnStart) for now.HaoK commentedon Jan 28, 2021
I like ValidateOnStart too, eagerly isn't clear exactly what that means
terrajobst commentedon Feb 4, 2021
Video
new()constraint? The existingOptionsBuilderDataAnnotationsExtensions.ValidateDataAnnotations()method doesn't have it.davidfowl commentedon Feb 4, 2021
👍🏾