Configuration in ASP.NET Core

With the release of ASP.NET Core, configuration was given a major upgrade. The new options API provides the ability to add configuration sections to the service provider and then inject them using the options interfaces. Unfortunately, I still see people injecting IConfiguration and then manually grabbing values with strings.

Getting Configuration with IConfiguration

There’s nothing wrong with using IConfiguration in the startup class to get configuration values, but there’s a right way and a wrong way to do it. Let’s start by looking at the wrong way to do it:


PasswordResetEmail = new Authentication.Options.╥AuthEmailMessageParametersß()
                    {
                        Sender = new Authentication.Options.╥AuthEmailUserParametersß()
                        {
                            Name = Configuration.GetSection("Email").GetSection("PasswordResetEmail").GetSection("Sender")["Name"],
                            EmailAddress = Configuration.GetSection("Email").GetSection("PasswordResetEmail").GetSection("Sender")["EmailAddress"]
                        },
                        Subject = Configuration.GetSection("Email").GetSection("PasswordResetEmail")["Subject"]
                    }

Reading through the code we can see the JSON configuration has an Email parent, followed by a PasswordResetEmail child and then another child. GetSection is used for getting a JSON object, and the indexer following it is used for getting specific properties. Now, given that we have everything in JSON, it should be obvious that we don’t need to create a new object and manually map every property through a string.

We already have JSON serialization and deserialization in .NET Core through JSON.NET. What ASP.NET Core has added is a new API to do it through options. Instead of using the above code, we can simply change it to the following:


PasswordResetEmail = Configuration.GetSection("Email").GetSection("PasswordResetEmail").Get<Authentication.Options.╥AuthEmailMessageParametersß>();

This will take the PasswordResetEmail section and deserialize it to our class directly. Since we aren’t injecting anything into our startup, using IConfiguration to get configuration in this case is the accepted approach. There’s no need for us to inject IConfiguration into our controllers or other classes though.

Injecting Options ASP.NET Core

Instead of injecting IConfigure we want to inject an IOptions interface into our controller instead. First thing we need to do is add our options class and its configuration section to the service provider.


services.AddOptions();
services.Configure(Configuration.GetSection("IdentityServerOptions");

You must call AddOptions first to setup the service provider for using the options feature. From there you call services.Configure using the same approach you used with IConfigure to get the section and associate it with your type. From here we inject our options using IOptions<T>.


╥IdentityServerOptionsß identityServerOptions;

public ResetPasswordRequest(«IOptionsß<╥IdentityServerOptionsß> identityServerOptions)
{
    this.identityServerOptions = identityServerOptions.Value;
}

Simple and straight forward. Just be sure to set your class to the value of the injected options.

Sean Leitzinger

Solutions Architect at Edgeside Solutions
.NET and C# aficionado with an interest in architecture, patterns, practices, and more. Microsoft fanatic.

Latest posts by Sean Leitzinger (see all)

Leave a Reply

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