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);
The preceding code loads the following data from the appsettings.json file:
{
“defaultOptions”: {
“name”: “Default Options”
}
}
The defaultOptions section maps to objects with the same key in the JSON file (highlighted code). The name property of the defaultOptions section translates to the Name property of the MyOptions class.That code does the same as the preceding hardcoded version. However, manually loading the section this way allows us to load a different section for different named options.Alternatively, we can also “bind” a configuration section to an existing object using the Bind method like this:
var options = new MyOptions();
builder.Configuration.GetSection(“options1”).Bind(options);
That code loads the settings and assigns them to the object’s properties, matching the settings key to the properties name. However, this does not add the object to the IoC container.To overcome this, if we do not want to register the dependency manually and don’t need the object, we can use the Bind or BindConfiguration methods from the OptionsBuilder<TOptions>. We create that object with the AddOptions method, like for Bind:
builder.Services.AddOptions<MyOptions>(“Options3”)
.Bind(builder.Configuration.GetSection(“options3”));
The preceding code loads the options3 configuration section using the GetSection method (highlighted), then the OptionsBuilder<TOptions> binds that value to the name Options3 through the Bind method. This registers a named instance of MyOptions with the container. We dig into named options later.Then again, we can skip the use of the GetSection method by using the BindConfiguration method instead, like this:
builder.Services.AddOptions<MyOptions>(“Options4”)
.BindConfiguration(“options4”);
The preceding code loads the settings from the options4 section, then registers that new setting with the IoC container.These are just a subset of the different ways we can leverage the ASP.NET Core Options pattern and configuration system. Now that we know how to configure the options, it is time to use them.
Injecting options
Let’s start by learning how to leverage the IOptions<TOptions> interface, the first and simplest interface that came out of .NET Core.To try this out, let’s create an endpoint and inject the IOptions<MyOptions> interface as a parameter:
app.MapGet(
“/my-options/”,
(IOptions<MyOptions> options) => options.Value
);
In the preceding code, the Value property returns the configured value, which is the following, serialized as JSON:
{
“name”: “Default Options”
}
And voilà! We can also use constructor injection or any other method we know to use the value of our options object.Next, we explore configuring multiple instances of the same options class.