---
source: https://iinventedlight.com/library/notes/dotnet-dependency-injection.md
title: "Dependency injection in .NET"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "CC BY 4.0 (dotnet/docs); prose restates or closely paraphrases cited source lines"
license_family: dotnet-docs-ccby
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "Summary by iinventedlight (AI-assisted). Sources: Microsoft Learn (.NET docs), Dependency injection - .NET, CC BY 4.0 (dotnet/docs); Microsoft Learn (.NET docs), Dependency injection basics quickstart - .NET, CC BY 4.0 (dotnet/docs)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: .NET"
tags: ['dotnet', 'dependency-injection', 'ioc']
sources:
  - id: S1
    title: "Dependency injection - .NET"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
  - id: S2
    title: "Dependency injection basics quickstart - .NET"
    publisher: Microsoft Learn (.NET docs)
    url: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-basics
    accessed: 2026-11-04
    license: "CC BY 4.0 (dotnet/docs)"
---
# Dependency injection in .NET

**Answer:** .NET supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies. Dependency injection in .NET is a built-in part of the framework, along with configuration, logging, and the options pattern. (S1)

## The problem it solves

- A dependency is an object that another object depends on. (S1)
- When a Worker class creates and directly depends on a MessageWriter class, replacing MessageWriter with a different implementation means you must modify the Worker class. (S1)
- In a large project with multiple classes depending on MessageWriter, the configuration code becomes scattered across the app. (S1)
- Dependency injection is a design pattern that you can use to remove hard-coded dependencies and make your application more maintainable and testable. (S2)

## The moving parts

- IServiceCollection defines a contract for a collection of service descriptors. (S2)
- IServiceProvider defines a mechanism for retrieving a service object. (S2)
- ServiceDescriptor describes a service with its service type, implementation, and lifetime. (S2)
- Services are typically registered at the app's start-up and appended to an IServiceCollection. (S1)
- After you register services, call the BuildServiceProvider method to build an IServiceProvider instance, which acts as a container for all the registered services. (S2)
- The AddSingleton method registers the service with a singleton lifetime, which means it isn't disposed until the app shuts down. (S2)

## Claims
- id: C1
  claim: DI is a technique for achieving Inversion of Control.
  source: S1
  evidence: "which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies."
- id: C2
  claim: A dependency is an object another object depends on.
  source: S1
  evidence: "A dependency is an object that another object depends on."
- id: C3
  claim: IServiceProvider retrieves service objects.
  source: S2
  evidence: "IServiceProvider: Defines a mechanism for retrieving a service object."
- id: C4
  claim: BuildServiceProvider builds an IServiceProvider.
  source: S2
  evidence: "call the BuildServiceProvider method to build an IServiceProvider instance."

## Sources
- [S1] Dependency injection - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.
- [S2] Dependency injection basics quickstart - .NET. Microsoft Learn (.NET docs). https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-basics . License: CC BY 4.0 (dotnet/docs). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
