liveBook · Manning

Chapter 9: Registering services with dependency injection

What you will learn in chapter 9 of ASP.NET Core in Action, 3e

This chapter introduces how to register custom services in ASP.NET Core DI, manage multiple registrations, and choose safe service lifetimes.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces how to register custom services in ASP.NET Core DI, manage multiple registrations, and choose safe service lifetimes.

Key terms: dependency injection, DI container, service registration, built-in DI container, dependency graph, multiple implementations, default service registration, TryAdd, service lifetime, transient

9.1 Registering custom services with the DI container

This section shows how to register custom services and their dependencies so ASP.NET Core's DI container can create them when they are requested.

Key terms: DI container, IServiceCollection, WebApplicationBuilder.Services, IEmailSender, EmailSender, AddScoped, service type, implementation type, lifetime, TService

9.2 Registering services using objects and lambdas

This section shows how to register services that the built-in DI container cannot construct directly, using instances, factory lambdas, open-generic registrations, and registration extension methods.

Key terms: built-in DI container, concrete type, relevant constructor, Add* methods, primitive types, singleton, scoped, factory pattern, lambda function, IServiceProvider

9.3 Registering a service in the container multiple times

This section introduces registering multiple implementations of one interface and previews different injection behavior when a consumer needs all implementations versus only one.

Key terms: interface, IEmailSender, EmailSender, SmsSender, FacebookSender, DI container, RegisterUser, consumer, implementation

9.3.1 Injecting multiple implementations of an interface

This section shows how to register several implementations of one interface and inject them all into an endpoint handler for collective use.

Key terms: IMessageSender, IEnumerable<IMessageSender>, DI container, Add* extension methods, RegisterUser, IEnumerable<T>, T[], ICollection<T>, InvalidOperationException

9.3.2 Injecting a single implementation when multiple services are registered

This section explains that when several implementations of a service are registered, the DI container injects the last registration for a single service request and how this can override library defaults.

Key terms: DI container, registered implementation, single instance, IMessageSender, FacebookSender, Add* extension method, custom implementation, IEnumerable<T>

9.3.3 Conditionally registering services using TryAdd

This section explains how to conditionally provide default DI services with TryAdd methods and how to replace an existing registration.

Key terms: TryAddScoped, Microsoft.Extensions.DependencyInjection.Extensions, IMessageSender, EmailSender, SmsSender, Replace(), lifetime

9.4 Understanding lifetimes: When are services created?

This section introduces DI service lifetimes, contrasts transient, scoped, and singleton reuse behavior, and sets up an example for observing their effects across dependency graphs and requests.

Key terms: lifetime, DI container, service registration, dependency, Transient, Scoped, Singleton, scope, web request, dependency graph

9.4.1 Transient: Everyone is unique

This section explains that transient DI services are newly created for every dependency resolution and are best limited to lightweight, mostly stateless services.

Key terms: transient services, transient lifetime, dependency, dependency graph, DI container, AddTransient, lightweight services, state, scoped

9.4.2 Scoped: Let’s stick together

This section explains how scoped services share one instance throughout an ASP.NET Core request and receive a new instance for each separate request.

Key terms: scoped lifetime, scope, request, DI container, AddScoped, scoped dependencies, DataContext, Repository, authentication services, IDisposable

9.4.3 Singleton: There can be only one

This section explains that singleton registration supplies one shared service instance for the application lifetime and requires that shared service to be thread-safe.

Key terms: singleton, DI container, service registration, DataContext, RowCounts, Repository, thread-safe, concurrent requests, caching service

9.4.4 Keeping an eye out for captive dependencies

This section explains how mismatched DI service lifetimes create captive dependencies and how ASP.NET Core validation settings help detect lifetime and registration errors.

Key terms: captive dependency, DataContext, Repository, scoped, singleton, transient, lifetime, ValidateScopes, ValidateOnBuild, WebApplicationBuilder

9.5 Resolving scoped services outside a request

This section explains how to safely resolve scoped or transient services outside an HTTP request by creating and disposing a DI scope manually.

Key terms: WebApplication.Services, root DI container, singleton, scoped, transient, GetRequiredService<T>(), IServiceProvider, IServiceScope, CreateScope(), ServiceProvider

9.6 Summary

This summary consolidates how to register services, select implementations and lifetimes, handle multiple registrations, and resolve scoped services outside requests.

Where this is used later

Concepts explained in this chapter

← Chapter 8: An introduction to dependency injection Chapter 10: Configuring an ASP.NET Core application →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.