liveBook · Manning

Bean scope

As explained in 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.

In ASP.NET Core dependency injection, a scoped service is created once within a scope and reused to resolve dependencies throughout that scope. ASP.NET Core starts a new scope for each HTTP request, so components handling one request can share an instance without sharing it with the next request. section 14.4.3

Choose the registration that matches the sharing you need:

  • AddSingleton<T, U>() uses one instance for subsequent dependencies. section 14.4
  • AddTransient<T, U>() creates a new instance for each dependency. section 14.4
  • AddScoped<T, U>() reuses an instance within one scope, such as an HTTP request. section 14.4

Check reuse with a GUID

Register `builder.Services.AddScoped<IResponseFormatter, GuidService>();` in Program.cs, then declare three `IResponseFormatter` parameters on a middleware `Invoke` method. Request `/middleware/class`: all three formatted messages show the same GUID. Reload the page: the new request gets a new GUID. With `AddTransient` instead, the three independently resolved parameters show different GUIDs even within one request. section 14.4.3

Resolve scoped services inside a scope. An endpoint helper that creates its endpoint from the application service provider during route setup can fail when it requests a scoped service: that work happens outside the request scope. Create and resolve the endpoint while handling the request instead. For work performed at application startup, create a scope with `CreateScope()` before requesting scoped services; Identity seeding needs this for `UserManager<T>` and `RoleManager<T>`. section 14.4.3 section 39.3.4

Filters can use the same lifetime. Register a filter with `AddScoped`, then obtain it as a service—for example, with `ServiceFilter`. Two uses during one request share the filter instance; reloading creates another. Entity Framework Core context scopes likewise need careful management to avoid stale data. section 30.5.2 section 36.6

How Pro ASP.NET Core 7, Tenth Edition covers it

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 it assumes
Assumes working knowledge of C# service registration and middleware while introducing how singleton, transient, and scoped lifetimes differ.
What you can do after
Operate/configure scoped ASP.NET Core services with AddScoped and create a scope before resolving them during startup.
How it connects
Connects dependency injection scopes to middleware, endpoint resolution, and MVC filters.

Also explained in

Compare how 3 books explain bean scope →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.