10 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 minimal API endpoints to create an HTTP 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 some previous approaches in old versions of .NET Framework. In section 10.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 10.3. This lets you nicely encapsulate settings for different features in your app.

10.1 Introducing the ASP.NET Core configuration model

10.2 Building a configuration object for your app

10.2.1 Adding a configuration provider in Program.cs

10.2.2 Using multiple providers to override configuration values

10.2.3 Storing configuration secrets safely

10.2.4 Reloading configuration values when they change

10.3 Using strongly typed settings with the options pattern

10.3.1 Introducing the IOptions interface

10.3.2 Reloading strongly typed options with IOptionsSnapshot

10.3.3 Designing your options classes for automatic binding

10.3.4 Binding strongly typed settings without the IOptions interface

10.4 Configuring an application for multiple environments

10.4.1 Identifying the hosting environment

10.4.2 Loading environment-specific configuration files

10.4.3 Setting the hosting environment

10.5 Summary

sitemap