Microsoft.Extensions.Options.DataAnnotations
provides additional DataAnnotations specific functionality related to Options..
Documentation can be found at https://learn.microsoft.com/en-us/dotnet/core/extensions/options.
As of .NET 6 Preview 2, we can validate options on application startup, to help us ensure we do not face misconfiguration issues during production in our applications. For example, for an app with settings below:
services
.AddOptions<MyOptions>()
.ValidateDataAnnotations()
.ValidateOnStart();
where
using System.ComponentModel.DataAnnotations;
public class MyOptions
{
[Required(AllowEmptyStrings = false)]
public string Name { get; set; }
}
upon startup we will get an error stating that the Name field is required.
Although the types are mature, the code base continues to evolve for better performance.
Microsoft.Extensions.Options.DataAnnotations is not included in the shared framework. The package is deployed as out-of-band (OOB) and needs to be installed into projects directly.