Category: Reloading options at runtime

Summary – Options, Settings, and Configuration

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

Project – OptionsValidationFluentValidation – Options, Settings, and Configuration

In this project, we validate options classes using FluentValidation. FluentValidation is a popular open-source library that provides a validation framework different from data annotations. We explore FluentValidation more in Chapter 15, Getting Started with Vertical Slice Architecture, but that should not hinder you from following this example.Here, I want to show you how to leverage […]

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

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

Loading the configuration – Options, Settings, and Configuration

Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter covers the .NET Options pattern, a building block of any application. .NET Core introduced new predefined mechanisms […]



         


          Terms of Use | Accessibility Privacy