liveBook · Manning

Chapter 31: Advanced configuration of ASP.NET Core

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

This chapter introduces advanced ways to customize ASP.NET Core’s middleware pipeline, options configuration, and dependency injection container when built-in approaches are insufficient.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces advanced ways to customize ASP.NET Core’s middleware pipeline, options configuration, and dependency injection container when built-in approaches are insufficient.

Key terms: middleware pipeline, Map, Use, Run, custom middleware, endpoint routing, OptionsBuilder<T>, IOptions<T>, builder pattern, DI

31.1 Customizing your middleware pipeline

This section introduces why and how custom middleware can handle terminal responses and cross-cutting concerns, including pipeline branching and endpoint integration.

Key terms: custom middleware, terminal middleware, cross-cutting concern, endpoint routing system, middleware pipeline, Run, Map, Use, lambda expressions, middleware components

31.1.1 Creating simple apps with the Run extension

This section shows how to use the Run extension to create terminal middleware that unconditionally generates an HTTP response.

Key terms: Run extension, middleware pipeline, short-circuits, terminal middleware, HttpContext, Request, Response, Content-Type, WriteAsync, endpoint routing

31.1.2 Branching middleware pipelines with the Map extension

This section explains how the Map extension branches an ASP.NET Core middleware pipeline by URL path prefix and the limitations and effects of those branches.

Key terms: Map extension, branch, main trunk, IApplicationBuilder, branch builder, branch pipeline, Run extension, WebApplication, string-prefix match, endpoint routing

31.1.3 Adding to the pipeline with the Use extension

This section shows how the Use extension adds inline middleware that can inspect or handle a request, conditionally invoke the remaining pipeline, and safely apply response-wide changes.

Key terms: Use extension, middleware, HttpContext, Func<Task>, next(), pipeline, short-circuit, /ping, health-check middleware, Response

31.1.4 Building a custom middleware component

This section shows how to encapsulate inline middleware as reusable custom middleware classes, including safe dependency injection and fixed-URL handlers.

Key terms: custom middleware component, RequestDelegate, Invoke, Use extension, reflection, dependency injection, singleton, scoped, transient, Invoke method

31.1.5 Converting middleware into endpoint routing endpoints

This section shows how to wrap an existing response-generating middleware component in a mini pipeline and map it as an endpoint so it can participate in endpoint routing.

Key terms: middleware component, mini middleware pipeline, IApplicationBuilder, IEndpointRouteBuilder, CreateApplicationBuilder(), Build(), endpoint routing, AuthorizationMiddleware, authorization metadata, route template

31.2 Using DI with OptionsBuilder and IConfigureOptions

This section shows how to configure strongly typed IOptions<T> with DI services by using OptionsBuilder<T> or IConfigureOptions<T> while preserving configuration order and DI lifetime safety.

Key terms: IOptions<T>, Configure<T>(), IConfiguration, configuration steps, CurrencyOptions, OptionsBuilder<T>, AddOptions<T>(), BindConfiguration(), Configure<TDep>, ICurrencyProvider

31.3 Using a third-party dependency injection container

This section explains why and when to replace ASP.NET Core’s built-in dependency injection container and shows the minimum configuration needed to use Lamar.

Key terms: built-in container, third-party DI container, Lamar, assembly scanning, automatic service registration, property injection, Lamar.Microsoft.DependencyInjection, WebApplicationBuilder.Host, UseLamar(), ServiceRegistry

31.4 Summary

This summary recaps advanced ways to shape middleware pipelines, configure options with dependency injection, and replace the default DI container.

Where this is used later

Concepts explained in this chapter

← Chapter 30: Building ASP.NET Core apps with the generic host and Startup Chapter 32: Building custom MVC and Razor Pages components →

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.