dotnet-csharp-dependency-injection

Register and resolve services in the Microsoft.Extensions.DependencyInjection container.

71|10|Updated Feb 11, 2026
One-click install
npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection-wshaddix
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-dependency-injection
Source: https://github.com/wshaddix/dotnet-skills/tree/main/skills/dotnet-csharp-dependency-injection
Command: npx skills add https://github.com/wshaddix/dotnet-skills --skill dotnet-csharp-dependency-injection-wshaddix

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers correctly register and resolve services using Microsoft's Dependency Injection (DI) container, preventing common pitfalls like captive dependencies and ensuring efficient application architecture.

Core Features & Use Cases

  • Service Lifetimes: Understand and apply Transient, Scoped, and Singleton lifetimes correctly.
  • Registration Patterns: Implement interface-implementation pairs, multiple implementations, and factory delegates.
  • Advanced Features: Utilize Keyed Services (net8.0+), the Decoration pattern, and Hosted Services.
  • Use Case: Ensure your DbContext is correctly scoped per request and that long-lived services don't inadvertently hold references to short-lived ones, preventing memory leaks and runtime errors.

Quick Start

Register services using interface-implementation pairs and appropriate lifetimes.

Frequently Asked Questions about dotnet-csharp-dependency-injection

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I register multiple implementations of the same interface in the .NET DI container?

To register multiple implementations in the .NET DI container, you can use collection registration patterns or factory delegates to resolve specific instances dynamically. Alternatively, .NET 8+ supports keyed services, allowing you to register and resolve multiple implementations of the same interface using distinct identifier keys.

What is the difference between Transient, Scoped, and Singleton service lifetimes in ASP.NET Core?

Service lifetimes in ASP.NET Core dictate instance lifespan: Transient creates a new instance every time, Scoped creates one per request, and Singleton creates a single application-wide instance. Correctly applying these prevents captive dependency issues where long-lived services trap short-lived ones.

Why does my Singleton service throw an exception when resolving a Scoped DbContext in .NET?

A Singleton service throwing an exception when resolving a Scoped DbContext indicates a captive dependency problem. In .NET dependency injection, a Singleton cannot safely depend on a Scoped service because the Scoped service becomes effectively trapped, leading to memory leaks and concurrency errors.

How do I implement the decoration pattern with Microsoft.Extensions.DependencyInjection?

To implement the decoration pattern with Microsoft.Extensions.DependencyInjection, you wrap an existing registered service with a decorator class using factory delegates or the built-in activation utilities. This allows adding behavior like logging or caching without modifying the original implementation's code.

Can I use keyed services to resolve dependencies dynamically in .NET 8?

Yes, you can use keyed services in .NET 8 to resolve dependencies dynamically by registering types with a specific key. This advanced feature in the Microsoft.Extensions.DependencyInjection container allows selecting between multiple implementations of an interface at resolution time using the provided key.

How do I implement background tasks using hosted services in ASP.NET Core?

To implement background tasks using hosted services in ASP.NET Core, you register a class implementing IHostedService or BackgroundService in the DI container. The application automatically starts and stops these services, managing long-running operations independently of web requests.