Options For Configuring ASP.NET Core Application Settings

With all of the controversy around project configurations within .NET Core, one thing that has managed to be consistently JSON-based has been the use of application settings configuration files in this new world. While JSON does read incredibly easily, it can very quickly become muddled if you are storing large, complex objects consisting of nested objects and arrays, each with multiple properties of different types, and so on.

Thankfully, you have options. And I don't mean options like going to use XML; I mean that there's a specific framework designed to tackle issues like these and make working with these potentially cumbersome configurations much more manageable.

Enter the Options Framework

The Options framework is a very basic framework that is specifically designed to handle accessing and configuring POCO settings within .NET Core, and it simply couldn't be easier to work with.

Now let's say that you have a configuration file for your application that looks like the following, 

  1. {  
  2.     "ApplicationLayout": {  
  3.         "LayoutChangingEnabled"true,  
  4.         "Layouts": [  
  5.             {  
  6.                 "Name""Standard",  
  7.                 "Modules": [  
  8.                     {  
  9.                         "Name""Foo",  
  10.                         "Order": 1  
  11.                     },  
  12.                     {  
  13.                         "Name""Bar",  
  14.                         "Order": 2  
  15.                     }  
  16.                 ]  
  17.             },  
  18.             {  
  19.                 "Name""Detailed",  
  20.                 "Modules": [  
  21.                     {  
  22.                         "Name""Foo",  
  23.                         "Order": 1  
  24.                     },  
  25.                     {  
  26.                         "Name""Bar",  
  27.                         "Order": 2  
  28.                     },  
  29.                     {  
  30.                         "Name""Admin",  
  31.                         "Order": 3  
  32.                     }  
  33.                 ]  
  34.             }  
  35.         ]  
  36.     }  
  37. }   

It's fairly simple. But if you wanted to access any of the individual items, it could potentially get pretty hairy drilling down into nested elements and muddle up your code. Thankfully, this is where the Options framework really shines.

Options will allow you to define a C# class to represent your configuration settings, and it will handle actually binding your existing JSON configuration to this class with just a single line of code. Let's see what this structure might look like for our previous example, 

  1. public class ApplicationConfiguration    
  2. {  
  3.     public ApplicationLayout Layout { get; set; }              
  4. }  
  5.   
  6. public class ApplicationLayout    
  7. {  
  8.     public bool LayoutChangingEnabled { get; set; }  
  9.     public Layout[] Layouts{ get; set; }  
  10. }  
  11.   
  12. public class Layout    
  13. {  
  14.     public string Name { get; set; }  
  15.     public Module[] Modules { get; set; }  
  16. }  
  17.   
  18. public class Module    
  19. {  
  20.     public string Name { get; set; }  
  21.     public int Order { get; set; }  
  22. }   

Now all that we need to do is simply include the Options NuGet package within our application:

Install-Package Microsoft.Extensions.Options

And then within the Startup.cs file, we can read our configuration file in as expected, 

  1. var config = new ConfigurationBuilder()    
  2.     .AddJsonFile("YourConfigurationFile.json")  
  3.     .Build();   

And then simply wire up an IOptions service that will allow your strongly-typed configuration to now be injected as a service anywhere within your application, 

  1. public void ConfigureServices(IServiceCollection services)    
  2. {  
  3.     // Omitted for brevity  
  4.   
  5.     services.AddOptions();  
  6.     services.Configure<ApplicationConfiguration>(config);  
  7. }   

With all of these measures in place, you can now simply inject your Options within various locations in your application, allowing you to cleanly access the data that you need from your configuration without drilling down through multiple layers or nested elements, 

  1. public class FooController    
  2. {  
  3.      private readonly ApplicationConfiguration _options;  
  4.   
  5.      public FooController(IOptions options)  
  6.      {  
  7.           // Access your options here   
  8.           var canChangeLayout = options.Layout.LayoutChangingEnabled; // "true"  
  9.      }  
  10. }   

Depending on your scenario, you may want to potentially override one of the values present within your settings. This is very easy to do and simply requires the use of a delegate after the initial configuration as seen below, 

  1. public void ConfigureServices(IServiceCollection services)    
  2. {  
  3.     // Omitted for brevity  
  4.   
  5.     services.Configure<ApplicationConfiguration>(config);  
  6.   
  7.     // Update the configuration  
  8.     services.Configure<ApplicationConfiguration>(options =>  
  9.     {  
  10.         options.Layout.LayoutChangingEnabled = false;  
  11.     });  
  12. }   

There are a variety of other more advanced use cases that you might find helpful depending on the complexity of your application such as using snapshots to detect configuration changes, binding to object graphs, and numerous other features which you can read more about in the documentation.

See Them in Action

The ASP.NET team and several members of the community have worked together to put examples of just about every type of configuration scenario on GitHub if you want to see how they look in action. I'd highly recommend browsing through them if you need to tackle some of the more advanced use-cases (e.g. using in-memory providers, snapshots, object graphs, etc.) within your applications.

Up Next
    Ebook Download
    View all
    Learn
    View all