Pro Code Guide

Developer’s Guide To Programming

HOME    CONTACT   ABOUT

How to read Configuration using IOptions Pattern in ASP.NET Core

Updated Feb 24, 2021 | 0 comments

In this article, we will learn about how to read configuration using IOptions Pattern in ASP.NET Core. We normally have many settings in our application that are different for each environment like third-party service URLs, database connection string, email settings, log parameters, etc. We normally set these values in some configuration files so that we are able to change these values without the need to recompile the application.

ASP.NET Core uses appsettings.json file for such settings and this file is read when the application starts. Also, we can configure to read code to read these settings even after startup, i.e. we can change values even when the application is running.

Ins ASP.NET Core there is more than one source to read configuration values from like Environment variables, settings file, command-line arguments, directory files, in-memory objects, etc.

This article will cover how to read configuration using IOptions pattern from appsettings.json file.

Options Pattern

Option pattern uses classes to bind a group of related configuration settings to a strong typed access i.e. class will have properties that correspond to some setting in the configuration file. These classes are registered in the dependency injection containers to be injected into services, controllers, etc using constructor dependency injection.

Options pattern helps in keeping settings of the different sections separate i.e. setting for logging can be separate from setting for database connectivity.

For details on dependency injection, you can read my other article on Dependency Injection in ASP.NET Core 3.1

Type of options interfaces

There are 3 type of interfaces supported by options pattern

IOptions<TOptions> – Reads configuration data once when the application starts and any changes in configuration will require the application to be restarted. It is registered in the dependency injection container with a singleton lifetime.

IOptionsSnapshot<TOptions> – Reads configuration on every request. Any changes in configuration while the application is running will be available for new requests without the need to restart the application. It is registered in the dependency injection container as a scoped lifetime. Being a scoped service it receives current options values at the time the object is constructed.

IOptionsMonitor<TOptions> – It is a combination of IOptions & IOptionsSnapshot. Supports re-binding configuration but it is only when configuration changes and not on every request like IOptionsSnapshot. It is registered in the dependency injection container as a singleton lifetime (same as IOptions).

Once the options pattern is configured then it can be injected via dependency injection using one of the available interfaces.

Implement IOptions Pattern

Here is a short & quick video on how to read configuration using IOptions pattern in ASP.NET Core

For a demonstration of options pattern to read configuration using IOptions Pattern in ASP.NET Core, we will be creating a Web API project and read configuration file values using options pattern

Create ASP.NET Core Project

To demonstrate read configuration using IOptions pattern in ASP.NET Core we will be creating a project of type API as shown below.

Create ASP.NET Core Web API Project

Add Parameters to appsettings.json

Lets add some sample settings in appsettings.json file to be read using IOptions Pattern

{
  "ApplicationParameters": {
    "SQLServerConnection": "SQL Server Connection String",
    "EmailServer": "127.0.0.1",
    "ServiceURL": "https://procodeguide.com/getpost",
    "MaxLimitUsers": 3000
  }
}

We have added setting section named ApplicationParameters & have added four parameters under it i.e. SQLServerConnection, EmailServer, ServiceURL & MaxLimitUsers

Add properties class

We will add a properties class that corresponds to the settings specified in the appsettings.json ApplicationParameters section.

public class ApplicationParameters
{
    public string SQLServerConnection { get; set; }
    public string EmailServer { get; set; }
    public string ServiceURL { get; set; }
    public long MaxLimitUsers { get; set; }
}

Bind configuration to your class in Startup class

Let’s add a service in the startup class to read configuration using IOptions Pattern in ASP.NET Core into an instance of properties class ApplicationParameters.

public class Startup
{
    public Startup(IWebHostEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        Configuration = builder.Build();
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services.Configure<ApplicationParameters>(
            this.Configuration.GetSection("ApplicationParameters"));
    }
    //Remaining code has been removed
}

The default order in which values are read from JSON file is

  1. appsettings.json
  2. appsettings.{environment}.json

i.e. if same key exists in both appsettings.json & appsettings.{environment}.json then on runtime it should display the value from appsettings.{environment}.json

Add a controller to read parameters using IOptions

The sample controller has been added to inject configuration in constructor injection using IOptions & IOptionsSnapshot interfaces. It has one action index which return values of configuration parameters read using IOptions & IoptionsSnapshot

[Route("api/[controller]")]
[ApiController]
public class AppParamtersController : ControllerBase
{
    private ApplicationParameters applicationParameters;
    private ApplicationParameters applicationParametersSnapshot;
    public AppParamtersController(IOptions<ApplicationParameters> options, IOptionsSnapshot<ApplicationParameters> optionsSnap)
    {
        applicationParameters = options.Value;
        applicationParametersSnapshot = optionsSnap.Value;
    }

    [HttpGet]
    public IActionResult Index()
    {
        StringBuilder response = new StringBuilder();
        response.AppendLine("IOptions ==>");
        response.AppendLine(JsonConvert.SerializeObject(applicationParameters));
        response.AppendLine("");
        response.AppendLine("IOptionsSnapshot ==>");
        response.AppendLine(JsonConvert.SerializeObject(applicationParametersSnapshot));
        return Content(response.ToString());
    }
}

In above controller we have are using IOptions pattern to read configuration values in ASP.NET Core.

Let’s run & test the code to read configuration using IOptions Pattern in ASP.NET Core

When we run the code and navigate to path API/AppParamters/Index then configuration values are displayed for IOption & IOptionsSnapshot. Both values match as it has been read when the app started.

IOptions Test Results 1

Now without stopping application modify values in appsettings.json to new values & save changes

Modified appsetting.json

Now refresh browser (without restarting application) and check values for both IOptions & IOptionsSnapshot. You will see that only IOptionsSnapshot contains the latest modified values as it is designed to read values before each request

read configuration values in asp.net core

IOptionsMonitor also works in a way similar to IOptionsSnapshot only difference being in the IOptionMonitor service lifetime is Singleton instead of Scoped as in IOptionsSnapshot.

Guidelines for storing configuration values

We saw how to read configuration using IOptions Pattern in ASP.NET Core but let’s understand few guidelines on do’s & don’ts that should be followed while storing values in configuration files.

  • All sensitive data like passwords, personal information should never be stored in plain text in configuration i.e. if at all it is being stored in configuration then it should be encrypted.
  • As far as possible production secrets should be different from development & staging secrets.
  • Use different naming conventions for configuration files on different environments like appsettings.development.json and appsettings.production.json.

Summary

We saw how to read configuration using IOptions Pattern in ASP.NET Core. We can inject IOption into service, controller, etc using constructor injection.

There are 3 types of IOptions interface IOptions, IOptionsSnapshot & IOptionsMonitor. IOptionsSnapshot & IOptionsMonitor allows you to read modified parameters even while the application is running.

References: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-3.1

Download Source Code

Here is the source code for demonstration of how to read configuration using IOptions Pattern in ASP.NET Core

Hope you found this article useful. Your support is appreciated!
Buy me a coffeeBuy me a coffee
Home 9 Programming 9 How to read Configuration using IOptions Pattern in ASP.NET Core

Set start URL in ASP.NET Core – Quick & Easy ways

This article will cover the ways to set start URL in ASP.NET Core 5 applications i.e. change the default URL (http://localhost:5000) in ASP.NET Core applications. When you create any new ASP.NET Core application whether its MVC App or Web API and run it then it will...

Hangfire in ASP.NET Core – Easy way to Schedule Background Jobs

This article covers detail about how to integrate Hangfire in ASP.NET Core applications which is an easy way to schedule background jobs in .NET Core and .NET based applications. Background jobs or tasks allow the programmers to execute code on a different thread but...

How to Send Emails in ASP.NET Core – Quick & Easy Guide

These days sending emails like user email id confirmation, OTP emails, acknowledgments, etc. is a very common requirement of any application. In this article, we will see a demo about how to send emails in ASP.NET Core in quick & easy steps. We will create an...

ML.NET – Machine Learning with .NET Core – Beginner’s Guide

This article will get you started with the fundamentals of Machine Learning and how to get started with Machine Learning with .NET Core i.e. ML.NET. We will even learn different concepts of Machine learning with a brief overview. Introduction to Machine Learning...

Implement Cookie Authentication in ASP.NET Core – Detailed Guide

This article will get you started with implementing cookie authentication in ASP.NET Core applications. Cookie authentication allows you to have your own login/register screens & custom logic for user-id/password validation without the need to use ASP.NET Core...

ASP.NET Core Identity Roles based Authorization

This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization. Role-based authorization is for basic authorization where...

Dependency Injection in ASP.NET Core 3.1 – Beginner’s Guide

This article will cover in details dependency injection in ASP.NET Core 3.1. ASP.NET Core is designed from the ground up to support the dependency injection design pattern. Dependency injection in ASP.NET Core provides a technique to create applications that are...

Real-time Web Applications with SignalR in ASP.NET Core 3.1

In this article, we will learn about real-time web & how to build real-time web applications using SignalR in ASP.NET Core. As part of this article, we will work on an application that can send real-time messages/alerts to all connected users or private messages...

Code Profiling using MiniProfiler in ASP.NET Core 3.1

This article will get you started with how to enable miniprofiler in ASP.NET Core to measure the performance of your application. MiniProlifer helps you understand how your application is performing by allowing you to profile any part of code in ASP.NET Core...

Microservices with ASP.NET Core 3.1

Microservices is a type of Architecture in which application is created as multiple small independent serviceable components. This article will cover in detail how to create microservices with ASP.NET Core, Serilog, Swagger UI, Health Checks & Docker containers....

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *