liveBook · Manning

AddScoped

As explained in ASP.NET Core in Action, 3e

AddScoped is a Microsoft.Extensions.DependencyInjection method that registers a service with a scoped lifetime in the dependency-injection container.

AddScoped registers a service in the ASP.NET Core dependency-injection container with a scoped lifetime. It tells the container which concrete type to create when an interface or service type is requested. section 9.1

Common registration forms include:

  • AddScoped<IEmailSender, EmailSender>() uses EmailSender whenever IEmailSender is required. section 9.1
  • AddScoped<NetworkClient>() registers the same concrete type as both service and implementation. section 9.1
  • A scoped registration shares one instance within a request and creates a new instance for the next request. section 9.4.2
  • Scoped services suit database contexts, authentication services, request-dependent services, and services that depend on scoped services. section 9.4.2

Register an email sender

builder.Services.AddScoped<IEmailSender, EmailSender>(); An endpoint can then request IEmailSender as a parameter. The container constructs EmailSender and its registered dependencies, such as NetworkClient. section 9.1

Listing 9.3 Registering services with the DI container
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddScoped<IEmailSender, EmailSender>();   #1
builder.Services.AddScoped<NetworkClient>();               #2
builder.Services.AddSingleton<MessageFactory>();           #3
 
WebApplication app = builder.Build();
 
app.MapGet("/register/{username}", RegisterUser);
 
app.Run();
 
string RegisterUser(string username, IEmailSender emailSender)
{
    emailSender.SendEmail(username);
    return $"Email sent to {username}!";
}
#1 Whenever you require an IEmailSender, use EmailSender.
#2 Whenever you require a NetworkClient, use NetworkClient.
#3 Whenever you require a MessageFactory, use MessageFactory.

Inspect a complete registration example showing interface mapping, concrete-type registration, and a separate singleton registration. section 9.1

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines overloads and request lifetime behavior, while deferring alternative factory and object registrations.
Examples
Uses EmailSender, NetworkClient, DataContext, database contexts, and authentication services to illustrate scoped registration.
What it assumes
Assumes basic C# types and interfaces while explaining concrete types, dependency registration, and request scopes.
What you can do after
Apply AddScoped to map services to implementations and share instances safely across one request.
How it connects
Connects dependency injection with service lifetimes, constructor dependencies, endpoint injection, and concrete implementations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.