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:
| |
Now, to bind this class to settings in appsettings.json, head over to ConfigureServices and add this line:
| |
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:
| |
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:
| |
Now, let’s wire this up in Startup.cs:
| |
Lastly, let’s use our provider in a controller, just to see those settings in action:
| |
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:
| |
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!
