dotnet-csharp-dependency-injection

Registers and resolves services with Microsoft.Extensions.DependencyInjection in .NET applications.

Updated May 22, 2026
One-click install
npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill dotnet-csharp-dependency-injection-viniciuscs84
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-dependency-injection
Source: https://github.com/viniciuscs84/sdd-toolkit/tree/main/skills/dotnet-csharp-dependency-injection
Command: npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill dotnet-csharp-dependency-injection-viniciuscs84

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Configuring dependency injection in .NET often leads to subtle bugs like captive dependencies, incorrect service lifetimes, and unorganized Program.cs files. This Skill provides proven patterns for registering, resolving, and organizing services with the built-in Microsoft.Extensions.DependencyInjection container. ## Core Features & Use Cases - Service Lifetime Management: Guidance on Transient, Scoped, and Singleton lifetimes, including how to avoid captive dependencies using IServiceScopeFactory and enable scope validation. - Advanced Registration Patterns: Keyed services (net8.0+), factory delegates, multiple implementations via IEnumerable, TryAdd for libraries, and decoration with manual wiring or the Scrutor library. - Hosted Services: Patterns for BackgroundService workers and IHostedService startup hooks with proper scope handling and exception management. - Use Case: When building an ASP.NET Core order processing API, use this Skill to register repositories and services with correct lifetimes, add a background queue processor worker, and organize all registrations into clean extension methods. ## Quick Start Ask the AI to register an order repository and service with scoped lifetime, add a background worker using BackgroundService, and organize the registrations into an extension method for Program.cs.

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 keyed services in .NET dependency injection?

Use AddKeyedScoped, AddKeyedSingleton, or AddKeyedTransient with a string key, then inject with the [FromKeyedServices] attribute or resolve via GetRequiredKeyedService. Keyed services require .NET 8 or later; earlier versions need factory or dictionary-based patterns.

How do I fix captive dependency errors in .NET DI?

A captive dependency occurs when a singleton captures a scoped service like DbContext. Fix it by injecting IServiceScopeFactory into the singleton and creating a scope per operation, then resolving the scoped service from that scope.

Does the built-in .NET DI container support decoration?

The built-in container does not natively support decoration. You can wire decorators manually with factory delegates, or use the Scrutor library's Decorate extension method to chain decorators around a registered service.

What is the difference between Transient, Scoped, and Singleton lifetimes?

Transient creates a new instance per injection and suits lightweight stateless services. Scoped creates one instance per request, ideal for DbContext or unit of work. Singleton creates one shared instance for the app lifetime, suited to thread-safe caches and configuration.

Why does my BackgroundService stop when an exception occurs?

In .NET 8 and later, unhandled exceptions in ExecuteAsync stop the host. Wrap the processing loop in a try-catch that ignores OperationCanceledException, log the error, and continue the loop with a delay between iterations.