← All notes

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

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

Sources

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

Permanent ID: iil:note:c76ks63x

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.},
}

Markdown

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.