So, you’ve got your Blazor app up and running, and it’s looking good — but wouldn’t it be even better with a bit more customization? Today, we’re going to dive into configuring scoped settings in ASP.NET Core, injecting them into providers, and adding some versatility (and security!) to your setup. By the end of this, your application will feel like it’s got its own personalized toolkit of settings.

Why Scoped Settings?
#

Think of scoped settings like your app’s favorite go-to references. These are settings specific enough to serve a particular part of your application yet dynamic enough to be swapped out based on environment or user preferences. We’ll use the IConfiguration interface in ASP.NET Core, which lets us pull configuration from appsettings.json, environment variables, user secrets, or other sources. Let’s get started.

Step 1: Configuring Scoped Settings
#

First, let’s create our settings class. Here’s an example that would work for an imaginary service called MyScopedSettings—it’s pretty straightforward:

1
2
3
4
5
public class MyScopedSettings
{
    public string Setting1 { get; set; }
    public int Setting2 { get; set; }
}

Now, to bind this class to settings in appsettings.json, head over to ConfigureServices and add this line:

1
2
3
4
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyScopedSettings>(Configuration.GetSection("MyScopedSettings"));
}

This setup tells ASP.NET Core to fetch the settings from a MyScopedSettings section in appsettings.json and bind them to MyScopedSettings class.

Step 2: Injecting Scoped Settings into Providers
#

Now, what’s a setting if it’s just sitting there all alone? Let’s inject it into a provider using the IOptions<T> interface so that our app can actually use it. Here’s how:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class MyProvider
{
    private readonly MyScopedSettings _settings;
    
    public MyProvider(IOptions<MyScopedSettings> options)
    {
        _settings = options.Value;
    }
    public void DoSomething()
    {
        Console.WriteLine($"Setting1: {_settings.Setting1}");
        Console.WriteLine($"Setting2: {_settings.Setting2}");
    }
}

Here, MyProvider accepts an IOptions<MyScopedSettings> in the constructor. The magical options.Value gives us access to those scoped settings we just configured.

Step 3: Putting It All Together
#

Let’s connect the dots. First, add some settings in your appsettings.json:

1
2
3
4
5
6
{
    "MyScopedSettings": {
        "Setting1": "Value1",
        "Setting2": 123
    }
}

Now, let’s wire this up in Startup.cs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class Startup
{
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<MyScopedSettings>(Configuration.GetSection("MyScopedSettings"));
        services.AddSingleton<MyProvider>();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // Configure the application pipeline
    }
}

Lastly, let’s use our provider in a controller, just to see those settings in action:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class MyController : ControllerBase
{
    private readonly MyProvider _provider;

    public MyController(MyProvider provider)
    {
        _provider = provider;
    }

    [HttpGet]
    public IActionResult Get()
    {
        _provider.DoSomething();
        return Ok();
    }
}

In this example, the MyController uses MyProvider, which has access to the settings in appsettings.json.

Bonus Round: Storing Sensitive Settings in Azure Key Vault
#

What if you’re dealing with sensitive information, like an API key or a secret sauce formula? It’s best not to leave it hanging around in appsettings.json. That’s where Azure Key Vault steps in, allowing you to securely manage and access sensitive information.

First, add the Azure Key Vault package:

dotnet add package Azure.Extensions.AspNetCore.Configuration.Secrets

Then, update Program.cs to include Key Vault configuration:

1
2
3
4
using Azure.Identity;  

var keyVaultEndpoint = new Uri("<Your-Key-Vault-URI>"); 
builder.Configuration.AddAzureKeyVault(keyVaultEndpoint, new DefaultAzureCredential());

With this setup, any secrets stored in Azure Key Vault that match keys in appsettings.json will automatically override those values. This lets you keep sensitive data securely stored without modifying your configuration logic.

Wrapping Up
#

Congratulations! You’ve configured scoped settings, injected them into providers, and even learned how to secure sensitive data with Azure Key Vault. By taking these steps, you’re making your application more versatile, secure, and professional.

See you in the next blog post, so stay tuned. And as always, happy coding!