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.
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.
IOptionsMonitor is a Singleton service that retrieves current option values at any time, which is especially useful in singleton dependencies.
} 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.
You need to create a class with MyOptions[] as a property and the inject that class and add the whole configuration without section.
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.
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");
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With