Loading configurations in .NET using the IOptions pattern in a Console App.

Marcio Nizzola - MVP
3 min readSep 7, 2023

--

Loading configurations in .NET using the IOptions pattern in a Console App.

Introduced from the first version of Asp.Net Core 1.0, using IOptions to load application settings has become much more practical and simple for passing parameters and variables between application layers.

The Microsoft Docs website has a detailed article on the options pattern in ASP.NET Core (link). This article covers various topics related to configuration in ASP.NET Core, such as named options, reloadable configuration, and configuring options using Dependency Injection.

The options pattern allows our application to follow two important software engineering principles: the Interface Segregation Principle (ISP) and separation of concerns. We use named options to decouple different configurations, so that services (classes) depend only on the specific settings they use.

One thing that can be challenging is configuration when using a console application. In this post, I’m demonstrating how to use it within a console application using dependency injection.

How to Use

I will use a real project to demonstrate how to use the pattern. To do this, let’s create a class with the necessary settings to use Azure Storage, and let’s call it “NizzolaAzureOptions.”

public class NizzolaAzureOptions
{
public string? Url { get; set; }
public string? ConnectionString { get; set; }
public string? ContainerName { get; set; }
}

Now that we have created the class, let’s create the configurations in the appsettings.json file to be assigned to this class during application initialization.

{
"AzureStorage": {
"ConnectionString": "DefaultEndpointsProtocol=https;AccountName=storageprodutos;AccountKey=x;EndpointSuffix=core.windows.net",
"ContainerName": "produtos",
"Url": "https://storageprodutos.blob.core.windows.net/"
}
}

Next, let’s modify the program.cs file for initialization to load the configuration data from appsettings and inject the loaded information into our class.

var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();

var services = new ServiceCollection();

services.Configure<NizzolaAzureOptions>(options => configuration.GetSection("AzureStorage").Bind(options));
services.AddSingleton<INizzolaAzureBlobService, NizzolaAzureBlobService>();

With this, the information loading is configured. Now, you just need to insert it into the constructor of the class that needs to use it and pass the settings directly to where they need to be used.

With this, the information loading is configured. Now, you just need to insert it into the constructor of the class that needs to use it and pass the settings directly to where they need to be used.

At this point, you can see that the following has been implemented in the console class code:

  1. Definition of the private property “_config,” which will receive the configuration class in the constructor, as readonly can only be input through the constructor.
  2. The point where dependency injection comes into action, bringing the configuration through the “IOptions” pattern.
  3. Assignment of the value, where you can see that the configuration is retrieved through the “value” property of the IOptions object.

There you go, you’ve learned how to use the Options pattern into a console application, which you can use in your projects going forward.

Did you enjoy the article? Click the 👏 icon and follow me to see upcoming publications! If you like to see more publications see my networks into my linktree: https://linktree.com/nizzola

References:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-7.0&WT.mc_id=DX-MVP-5004978

--

--

Marcio Nizzola - MVP

Software Architect, Microsoft MVP, Technology Teacher - .NET, Azure, Aws and other development technologies.