# dotnet-csharp-dependency-injection

dotnet-csharp-dependency-injection guides Microsoft DI in .NET, from service lifetimes and keyed registrations to decoration and hosted workers.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 125 installs · ★ 92 repo stars
- Author: wshaddix
- License: not stated · policy=link-only · description only; the file stays with the author
- Source: https://github.com/wshaddix/dotnet-skills/tree/HEAD/skills/dotnet-csharp-dependency-injection/SKILL.md
- Updated: 2026-02-27
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/dotnet-csharp-dependency-injection

## What it does

This skill covers common and advanced Microsoft.Extensions.DependencyInjection patterns. It explains how to choose transient, scoped, or singleton lifetimes, connect interfaces to implementations, register multiple implementations, and build factory-based registrations. It also addresses `TryAdd`, scope checks, and hosted services.

For projects targeting `net8.0+`, the skill shows keyed service registration and lookup. Its worker examples use `BackgroundService` with `IServiceScopeFactory` so a background loop can resolve scoped work inside a fresh scope. Since the built-in container has no native decoration support, the material uses manual wrappers or Scrutor for that case.

Keyed services require `net8.0+`; older target frameworks need a factory pattern or a dictionary-based design. Check this constraint before choosing keyed registration.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/dotnet-csharp-dependency-injection/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/dotnet-csharp-dependency-injection/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/dotnet-csharp-dependency-injection/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/dotnet-csharp-dependency-injection/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install wshaddix/dotnet-skills skills/dotnet-csharp-dependency-injection
```

2. **Manually: put the files in place**
- File: `.github/skills/dotnet-csharp-dependency-injection/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/wshaddix/dotnet-skills --path skills/dotnet-csharp-dependency-injection
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/dotnet-csharp-dependency-injection/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/wshaddix/dotnet-skills/tree/HEAD/skills/dotnet-csharp-dependency-injection/SKILL.md

## When to use it
- choose a suitable .NET service lifetime
- spot a scoped dependency captured by a singleton
- register several implementations of one interface
- resolve a service by key on `net8.0+`
- run scoped processing from a hosted worker

## Example prompts
- Review this .NET project’s DI setup and find lifetime mismatches.
- Rewrite these named cache registrations with keyed services for `net8.0+`.
- Create a `BackgroundService` that resolves `IQueueProcessor` through `IServiceScopeFactory`.

## Similar
- [dependency-injection](https://affpapa.org/en/nevibecode/skills/dependency-injection.md) — skill · 4000 installs
- [dependency-injection-patterns](https://affpapa.org/en/nevibecode/skills/dependency-injection-patterns.md) — skill · 715 installs
- [maui-dependency-injection](https://affpapa.org/en/nevibecode/skills/maui-dependency-injection.md) — skill · 4200 installs
- [openapi-to-application-csharp-dotnet](https://affpapa.org/en/nevibecode/plugins/openapi-to-application-csharp-dotnet.md) — plugin · ★ 41430 repo stars
- [csharp-dotnet-development](https://affpapa.org/en/nevibecode/plugins/csharp-dotnet-development.md) — plugin · ★ 41430 repo stars
- [golang-dependency-injection](https://affpapa.org/en/nevibecode/skills/golang-dependency-injection.md) — skill · 41700 installs

From the same set: [wshaddix/dotnet-skills](https://affpapa.org/en/nevibecode/by/wshaddix/dotnet-skills.md) — 24 cards

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
