11 Configuring an ASP.NET Core application

 

This chapter covers

  • Loading settings from multiple configuration providers
  • Storing sensitive settings safely
  • Using strongly typed settings objects
  • Using different settings in different hosting environments

In part 1 of this book, you learned the basics of getting an ASP.NET Core app up and running and how to use the MVC design pattern to create a traditional web app or a Web API. Once you start building real applications, you will quickly find that you want to tweak various settings at deploy time, without necessarily having to recompile your application. This chapter looks at how you can achieve this in ASP.NET Core using configuration.

I know. Configuration sounds boring, right? But I have to confess, the configuration model is one of my favorite parts of ASP.NET Core. It’s so easy to use and so much more elegant than the previous version of ASP.NET. In section 11.3 you’ll learn how to load values from a plethora of sources—JSON files, environment variables, and command-line arguments—and combine them into a unified configuration object.

On top of that, ASP.NET Core brings the ability to easily bind this configuration to strongly typed options objects. These are simple POCO classes that are populated from the configuration object, which you can inject into your services, as you’ll see in section 11.4. This lets you nicely encapsulate settings for different features in your app.

11.1 Introducing the ASP.NET Core configuration model

11.2 Configuring your application with CreateDefaultBuilder

11.3 Building a configuration object for your app

11.3.1 Adding a configuration provider in Program.cs

11.3.2 Using multiple providers to override configuration values

11.3.3 Storing configuration secrets safely

11.3.4 Reloading configuration values when they change

11.4 Using strongly typed settings with the options pattern

11.4.1 Introducing the IOptions interface

11.4.2 Reloading strongly typed options with IOptionsSnapshot

11.4.3 Designing your options classes for automatic binding

11.4.4 Binding strongly typed settings without the IOptions interface

11.5 Configuring an application for multiple environments

11.5.1 Identifying the hosting environment

11.5.2 Loading environment-specific configuration files

Summary