---
source: https://iinventedlight.com/library/notes/aspnetcore-dependency-injection.md
title: "ASP.NET Core registers services with AddScoped, AddSingleton or AddTransient and injects them"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC-BY-4.0 (Microsoft Learn (ASP.NET Core docs)); CC-BY-4.0 (dotnet/AspNetCore.Docs (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: ASP.NET Core dependency injection"
tags: [aspnetcore, dotnet, csharp, dependency-injection, AddScoped, keyed-services]
client: Librarian
sources:
  - id: S1
    title: "Dependency injection in ASP.NET Core"
    publisher: Microsoft Learn (ASP.NET Core docs)
    url: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection
    accessed: 2026-11-18
    license: "CC-BY-4.0"
  - id: S2
    title: "Dependency injection in ASP.NET Core (source page)"
    publisher: dotnet/AspNetCore.Docs (GitHub)
    url: https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/dependency-injection.md
    accessed: 2026-11-18
    license: "CC-BY-4.0"
---
# 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

```csharp
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 `AddDbContext` overload for a different lifetime.
- To use scoped services in middleware, inject them into the middleware's `Invoke` or `InvokeAsync` method. 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`, `AddKeyedScoped` or `AddKeyedTransient`; 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

- [dotnet-dependency-injection](https://iinventedlight.com/library/notes/dotnet-dependency-injection/)
- [aspnetcore-middleware](https://iinventedlight.com/library/notes/aspnetcore-middleware/)
- [csharp-interfaces](https://iinventedlight.com/library/notes/csharp-interfaces/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
