Year: 2023

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 […]

Project – OptionsConfiguration – Options, Settings, and Configuration

Now that we have covered basic usage scenarios, let’s attack some more advanced possibilities, such as creating types to configure, initialize, and validate our options.We start by configuring options which happen in two phases: In a nutshell, the post-configuration phase happens later in the process. This is a good place to enforce that some values […]

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 […]

Named options – Options, Settings, and Configuration

Now, let’s explore named options by configuring two more instances of the MyOptions class. The concept is to associate a configuration of the options with a name. Once that is done, we can request the configuration we need. Unfortunately, the ways we explore named options and most online examples break the Inversion of Control principle. […]

Using the settings file – Options, Settings, and Configuration

Loading configurations from a file is often more convenient than hardcoding the values in C#. Moreover, the mechanism allows overriding the configurations using different sources, bringing even more advantages.To load MyOptions from the appsettings.json file, we must first get the configuration section, then configure the options, like the following: var defaultOptionsSection = builder.Configuration    .GetSection(“defaultOptions”);builder.Services    .Configure<MyOptions>(defaultOptionsSection); […]

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 […]



         


          Terms of Use | Accessibility Privacy