Exploring common usage scenarios
This first example covers multiple basic use cases, such as injecting options, using named options, and storing options values in settings.
The name of the project in the source code is CommonScenarios
.
Let’s start by manually changing the configuration values.
Manual configuration
In the composition root, we can manually configure options, which is very useful for configuring ASP.NET Core MVC, the JSON serializer, other pieces of the framework, or our own handcrafted options.
Here’s the first options class we use in the code, which contains only a Name
property:
namespace CommonScenarios;
public class MyOptions
{
public string? Name { get; set; }
}
In the composition root, we can use the Configure
extension method, which extends the IServiceCollection
interface to configure our object. Here’s how we can set the default options of the MyOptions
class:
builder.Services.Configure<...