Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net-Core option validation

I have written an extension method to IServiceCollection which takes an option delegate. My problem here is that I have to validate the configured options first (e.g. filter out null values) since it's unsafe to proceed and instantiate a service that relies on these options.

public static class ServiceCollectionExtensions {
    public static void AddServices(
        this IServiceCollection services,
        Action<ServiceOptions> configureOptions)
    {
        // Configure service
        services.AddSingleton<IAbstraction, Implementation>();

        // Validate options here...

        // Configure options
        services.Configure(configureOptions);
    }
}

How can I validate here that the options are correctly specified without calling the delegate configureOptions? I don't want to rely on default values from ServiceOptions since I want to make some settings mandatory.

like image 680
Bruno Zell Avatar asked Apr 04 '18 12:04

Bruno Zell


People also ask

What is option in ASP.NET Core?

The options patterns provide an elegant way to add strongly typed settings to your ASP.NET Core application. The options pattern, which is an extension on top of the IServiceCollection interface, takes advantage of classes to represent a group of related settings.

Is Ioption a singleton?

IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies.

What is IServiceCollection in .NET Core?

} IServiceCollection is the collection of the service descriptors. We can register our services in this collection with different lifestyles (Transient, scoped, singleton) IServiceProvider is the simple built-in container that is included in ASP.NET Core that supports constructor injection by default.

How do you inject IOptionsMonitor?

You need to create a class with MyOptions[] as a property and the inject that class and add the whole configuration without section.


1 Answers

ASP.NET Core 2.2+

docs

There is a validation method on the OptionsBuilder. The validation will occur on the first use of the IOptions<T>.Value property. It will throw an OptionsValidationException if not valid. Eager validation is tracked here.

public static class ServiceCollectionExtensions {
    public static void AddServices(
        this IServiceCollection services,
        Action<ServiceOptions> configureOptions)
    {
        // Configure service
        services.AddSingleton<IAbstraction, Implementation>();

        // Configure and validate options
        services.AddOptions<ServiceOptions>()
            .Configure(configureOptions)
            .Validate(options => {
                // Take the fully configured options and return validity...
                return options.Option1 != null;
            });
    }
}

Alternatively, .ValidateDataAnnotations() is available too so the data annotation attributes are respected.

ASP.NET Core 2.0+

From ASP.NET Core 2.0 onwards, PostConfigure is a good fit. This function takes a configuration delegate too but is executed last, so everything is already configured.

public static class ServiceCollectionExtensions {
    public static void AddServices(
        this IServiceCollection services,
        Action<ServiceOptions> configureOptions)
    {
        // Configure service
        services.AddSingleton<IAbstraction, Implementation>();

        // Configure and validate options
        services.Configure(configureOptions);
        services.PostConfigure<ServiceOptions>(options => {
            // Take the fully configured options and run validation checks...
            if (options.Option1 == null) {
                throw new Exception("Option1 has to be specified");
            }
        });
    }
}
like image 117
Bruno Zell Avatar answered Sep 28 '22 03:09

Bruno Zell