liveBook · Manning

Bean scope

A dependency-injection scope defines the lifetime and reuse of a service or bean instance within a bounded context.

How 3 Manning books explain it

ASP.NET Core in Action, 3e

A scoped lifetime reuses one service instance within a dependency-injection scope and creates different instances for different scopes.

How it is used
Uses scoped services to share request-specific dependencies within an ASP.NET Core request without sharing them across requests.
How deep it goes
Explains registration, per-scope reuse, disposal, captive dependencies, and manual scope creation with a worked dependency graph.
Examples
Compares a handler and repository sharing a DataContext, and registers an email sender and its dependencies.
What you can do after
Apply it via the ASP.NET Core DI container by registering scoped services and creating scopes outside requests.

Read how ASP.NET Core in Action, 3e explains bean scope →

Pro ASP.NET Core 7, Tenth Edition

A scoped service is a dependency-injection service whose instance is reused within a defined scope, such as one HTTP request scope.

How it is used
Controls sharing of dependency-injection service instances among components handling an ASP.NET Core request.
How deep it goes
Explains registration, request-level reuse, manual scopes, and resolution outside a scope through observable behavior rather than container internals.
Examples
Compares GUIDs from three middleware dependencies across requests and from two filter uses in one request.
What you can do after
Operate/configure scoped ASP.NET Core services with AddScoped and create a scope before resolving them during startup.

Read how Pro ASP.NET Core 7, Tenth Edition explains bean scope →

Building Web APIs with ASP.NET Core

A scoped service is a dependency-injected service instance whose lifetime is limited to a single scope, typically one HTTP request in ASP.NET Core.

How it is used
Registers ApplicationDbContext for one unit of work while processing a batch rename operation.
How deep it goes
Explains scoped registration, SaveChanges timing, change detection, persistence, and disposal before addressing connection-string security.
Examples
Uses ApplicationDbContext in Program.cs with a SQL Server DefaultConnection for the MyBGList API.
What you can do after
Operate/configure ApplicationDbContext as a scoped service and protect its connection string with development settings or environment variables.

Read how Building Web APIs with ASP.NET Core explains bean scope →

Also covered in

What to know first

Related concepts

Questions about bean scope

What is bean scope?

A dependency-injection scope defines the lifetime and reuse of a service or bean instance within a bounded context.

Which Manning books explain bean scope?

ASP.NET Core in Action, 3e, Pro ASP.NET Core 7, Tenth Edition and Building Web APIs with ASP.NET Core.

What should I know before learning bean scope?

Manning books that teach bean scope usually introduce dependency injection and request pipeline first.

What kinds of bean scope are there?

Kinds of bean scope include application scope and prototype scope.

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.