Module 04 of 12

Dependency Injection & Configuration

Lessons

About This Module

Two features quietly shape every ASP.NET Core application: dependency injection and configuration. Instead of creating its own collaborators, a class asks for what it needs and the built-in DI container supplies it — which keeps code loosely coupled and easy to test. Configuration does the same for settings, layering values from files, environment variables, and secrets.

This module starts with dependency injection and service lifetimes — singleton, scoped, and transient — followed by a lesson on the classic pitfall of injecting a scoped service into a singleton. It then covers reading appsettings.json with IConfiguration and the options pattern, and finishes with user secrets, so sensitive values never end up in source control.

Watch the lessons in order, then continue on to Middleware & the Request Pipeline.

Lessons

4 videos
01

Dependency Injection in ASP.NET Core: Singleton vs Scoped vs Transient

A walkthrough of dependency injection in ASP.NET Core that breaks down the singleton, scoped, and transient lifetimes, how each works, and when to use each.

02

Why Singleton Hates Scoped Injection

Most services can inject other services regardless of lifetime, but not all combinations are safe. This lesson explains why a singleton can't safely depend on a scoped service.

03

How Should You Get Config Values from appsettings.json?

Compares injecting IConfiguration, IOptions, and IOptionsSnapshot into your API controller to read settings, so you can choose the right approach.

04

How to Use User Secrets in ASP.NET Core

Shows how to keep passwords, API keys, and connection strings out of appsettings.json using the dotnet user-secrets CLI, where secrets.json is stored, and how ASP.NET Core loads secrets automatically in Development.