dotnet-csharp-dependency-injection

Register and resolve services with Microsoft.Extensions.DependencyInjection in .NET applications.

1|Updated Feb 22, 2026
One-click install
npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-csharp-dependency-injection-rudironsoni
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-dependency-injection
Source: https://github.com/rudironsoni/dotnet-agent-harness/tree/main/.rulesync/skills/dotnet-csharp-dependency-injection
Command: npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-csharp-dependency-injection-rudironsoni

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill simplifies the complex task of managing service lifecycles, registrations, and resolutions within .NET applications, ensuring efficient and maintainable code.

Core Features & Use Cases

  • Service Lifetimes: Understand and implement Transient, Scoped, and Singleton lifetimes correctly.
  • Advanced Registration: Learn patterns for interface-implementation pairs, multiple implementations, factory delegates, and keyed services (.NET 8+).
  • Decoration & Hosted Services: Implement the decorator pattern and register background workers effectively.
  • Use Case: You need to register a DbContext with a scoped lifetime and ensure it's correctly injected into services that handle individual web requests, preventing lifetime mismatches.

Quick Start

Register an IOrderService as a scoped service with its implementation OrderService using the provided extension method.

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 services with different lifetimes in ASP.NET Core dependency injection?

ASP.NET Core dependency injection supports Transient, Scoped, and Singleton service lifetimes. You register services using extension methods to ensure correct instantiation, maintaining efficient and maintainable code across individual web requests.

What is a captive dependency and how do I prevent it in .NET DI?

A captive dependency occurs when a service with a shorter lifetime is injected into a service with a longer lifetime. You prevent it in .NET DI by enabling scope validation to detect lifetime mismatches during application startup.

How do I use keyed services for dependency injection in .NET 8?

Keyed services in .NET 8 dependency injection allow registering multiple implementations of an interface and resolving them by a specific key. This advanced registration pattern simplifies selecting the correct service implementation during resolution.

Can I use factory delegates to register services in Microsoft.Extensions.DependencyInjection?

Yes, Microsoft.Extensions.DependencyInjection supports factory delegates for advanced service registration. This allows you to provide a custom factory function to manually instantiate services during dependency resolution, offering fine-grained control.

What is the best way to implement the decorator pattern with .NET dependency injection?

The decorator pattern in .NET dependency injection wraps an existing service implementation with additional behavior. You achieve this by registering the decorated service and then wrapping it during resolution to augment functionality without modifying original code.

How do I register a background worker as a hosted service in ASP.NET Core?

You register a background worker as a hosted service in ASP.NET Core by adding it to the dependency injection container. This ensures the worker is started and stopped correctly by the application's host lifetime management infrastructure.