ASP.NET Core registers services with AddScoped, AddSingleton or AddTransient and injects them
Answer: Services are added to the service container, usually in the Program file (.NET 6 or later). AddScoped gives a scoped lifetime: a single request in MVC or Razor Pages apps, or a Blazor circuit in .NET 8 or later.
Example
builder.Services.AddScoped<IMyDependency, MyDependency>();
builder.Services.AddSingleton<MyDependency>();
builder.Services.AddKeyedSingleton<IStringCache, BigCache>("cache2");
public class MyHub1([FromKeyedServices("cache2")] IStringCache cache) : Hub { }
Rules
- Registering only an implementation type, as in
AddSingleton<MyDependency>(), equals registering it with the same implementation and service type. - Entity Framework contexts are added with the scoped lifetime by default, because web app database operations are normally scoped to the client request. Use an
AddDbContextoverload for a different lifetime. - To use scoped services in middleware, inject them into the middleware's
InvokeorInvokeAsyncmethod. Constructor injection would force the scoped service to behave like a singleton, so it throws a runtime exception. - Keyed services associate a service with a key using
AddKeyedSingleton,AddKeyedScopedorAddKeyedTransient; consumers pick one with[FromKeyedServices("key")].
Startup caution
Do not build a temporary service container during startup: a singleton created there could be different from one created in the final service container.
See also
Claims
- id: C1 claim: AddScoped registers a service with a scoped lifetime. source: S2 evidence: "method registers the service with a scoped lifetime"
- id: C2 claim: EF contexts use the scoped lifetime by default. source: S1 evidence: "Entity Framework contexts are added to the service container using the scoped lifetime"
- id: C3 claim: Constructor-injecting a scoped service into middleware throws. source: S1 evidence: "Using constructor injection throws a runtime exception"
- id: C4 claim: A temporary container can create a different singleton. source: S2 evidence: "a singleton service created in the temporary service container could be different"
Sources
- [S1] Dependency injection in ASP.NET Core. Microsoft Learn (ASP.NET Core docs). https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection . License: CC-BY-4.0. Accessed 2026-11-18.
- [S2] Dependency injection in ASP.NET Core (source page). dotnet/AspNetCore.Docs (GitHub). https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/dependency-injection.md . License: CC-BY-4.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (ASP.NET Core docs), Dependency injection in ASP.NET Core; dotnet/AspNetCore.Docs (GitHub), Dependency injection in ASP.NET Core (source page). CC-BY-4.0 (https://raw.githubusercontent.com/dotnet/AspNetCore.Docs/main/LICENSE). Copyright Microsoft Corporation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (ASP.NET Core docs), Dependency injection in ASP.NET Core; dotnet/AspNetCore.Docs (GitHub), Dependency injection in ASP.NET Core (source page). CC-BY-4.0 (https://raw.githubusercontent.com/dotnet/AspNetCore.Docs/main/LICENSE). Copyright Microsoft Corporation.
Cite this
iinventedlight. 2026. "ASP.NET Core registers services with AddScoped, AddSingleton or AddTransient and injects them." iinventedlight Library, iil:note:c76ks63x, version of 2026-11-18. https://iinventedlight.com/library/notes/aspnetcore-dependency-injection/. License: CC-BY-4.0.
@misc{iil_note_c76ks63x,
author = {{iinventedlight}},
title = {{ASP.NET Core registers services with AddScoped, AddSingleton or AddTransient and injects them}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/aspnetcore-dependency-injection/},
note = {Permanent ID iil:note:c76ks63x. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/aspnetcore-dependency-injection.md.