This chapter explored the Options pattern, a powerful tool allowing us to configure our ASP.NET Core applications. It enables us to change the application without altering the code. The capability even allows the application to reload the options at runtime when a configuration file is updated without downtime. We learned to load settings from multiple […]
Using the ValidateOptionsResultBuilder class – Options, Settings, and Configuration
The ValidateOptionsResultBuilder is a new type in .NET 8. It allows to dynamically accumulate validation errors and create a ValidateOptionsResult object representing its current state.Its basic usage is straightforward, as we are about to see. Project – ValidateOptionsResultBuilder In this project, we are validating the MyOptions object. The type has multiple validation rules, and we […]
Using the configuration-binding source generator – Options, Settings, and Configuration
.NET 8 introduces a configuration-binding source generator that provides an alternative to the default reflection-based implementation. In simple terms, the name of the options class properties and the settings keys are now hard-coded, accelerating the configuration retrieval. Beware, the settings keys are case-sensitive and map one-on-one with the C# class property name, unlike the non-generated […]
Validation types – Options, Settings, and Configuration
To implement options validation types or options validators, we can create a class that implements one or more IValidateOptions<TOptions> interfaces. One type can validate multiple options, and multiple types can validate the same options, so the possible combinations should cover all use cases.Using a custom class is no harder than using data annotations. However, it […]
Eager validation – Options, Settings, and Configuration
Eager validation has been added to .NET 6 and allows catching incorrectly configured options at startup time in a fail-fast mindset.The Microsoft.Extensions.Hosting assembly adds the ValidateOnStart extension method to the OptionsBuilder<TOptions> type.There are different ways of using this, including the following, which binds a configuration section to an options class: services.AddOptions<Options>() .Configure(o => /* Omitted […]
Exploring other configuration possibilities – Options, Settings, and Configuration
We can mix those configuration classes with extension methods. For example: Here, we use the PostConfigure method to demonstrate that. Let’s add the following two lines of code (highlighted): const string NamedInstance = “MyNamedInstance”;var builder = WebApplication.CreateBuilder(args);builder.Services.PostConfigure<ConfigureMeOptions>( NamedInstance, x => x.Lines = x.Lines.Append(“Inline PostConfigure Before”));builder.Services .AddSingleton<IPostConfigureOptions<ConfigureMeOptions>, ConfigureAllConfigureMeOptions>() .Configure<ConfigureMeOptions>(builder.Configuration .GetSection(“configureMe”)) .Configure<ConfigureMeOptions>(NamedInstance, builder.Configuration .GetSection(“configureMe”)) .AddSingleton<IConfigureOptions<ConfigureMeOptions>, ConfigureAllConfigureMeOptions>() //.AddSingleton<IConfigureNamedOptions<ConfigureMeOptions>, […]
Adding post-configuration – Options, Settings, and Configuration
We must take a similar path to add post-configuration values but implement the IPostConfigureOptions<TOptions> instead. To achieve this, we update the ConfigureAllConfigureMeOptions class to implement that interface: namespace OptionsConfiguration;public class ConfigureAllConfigureMeOptions : IPostConfigureOptions<ConfigureMeOptions>, IConfigureNamedOptions<ConfigureMeOptions>{ // Omitted previous code public void PostConfigure(string?name, ConfigureMeOptions options) { options.Lines = options.Lines.Append( $”ConfigureAll:PostConfigure name: {name}”); }} In the preceding code, […]
Configuring the options – Options, Settings, and Configuration
We want two different options to test out many possibilities: To achieve this, we must add the following lines in the Program.cs file: const string NamedInstance = “MyNamedInstance”;builder.Services .Configure<ConfigureMeOptions>(builder.Configuration .GetSection(“configureMe”)) .Configure<ConfigureMeOptions>(NamedInstance, builder.Configuration .GetSection(“configureMe”)); The preceding code registers a default instance (highlighted code) and a named instance. Both use the configureMe configuration sections and so start […]
Reloading options at runtime – Options, Settings, and Configuration
A fascinating aspect of the ASP.NET Core options is that the system reloads the value of the options when someone updates a configuration file like appsettings.json. To try it out, you can: This is an out-of-the-box feature. However, the system rebuilds the options instance, which does not update the references on the previous instance. The […]
IOptionsFactory – Options, Settings, and Configuration
This interface is a factory, as we saw in Chapter 7, Strategy, Abstract Factory, and Singleton, and in Chapter 8, Dependency Injection, we use factories to create instances; this interface is no different. Unless necessary, I suggest sticking with IOptionsMonitor<TOptions> or IOptionsSnapshot<TOptions> instead. How the factory works is simple: the container creates a new factory […]