dependency-injection

Configure dependency injection registrations and lifetimes for .NET applications.

4|1|Updated Mar 15, 2026
One-click install
npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill dependency-injection-faysilalshareef
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dependency-injection
Source: https://github.com/FaysilAlshareef/dotnet-ai-kit/tree/main/skills/core/dependency-injection
Command: npx skills add https://github.com/FaysilAlshareef/dotnet-ai-kit --skill dependency-injection-faysilalshareef

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Inconsistent or incorrect dependency registrations lead to runtime errors, captive dependencies, thread-safety issues, and hard-to-debug lifetime problems; this Skill provides clear rules and patterns to register services correctly and avoid common DI anti-patterns.

Core Features & Use Cases

  • Constructor injection guidance to eliminate service locator usage and make dependencies explicit.
  • Lifetime decision guide for choosing between Singleton, Scoped, and Transient to prevent captive dependencies.
  • Registration organization using IServiceCollection extension methods to modularize layer-specific services.
  • Advanced patterns including keyed services for multiple implementations, decorator registration, factory delegates, and background service scoping with IServiceScopeFactory.
  • Options and validation recommendations such as ValidateOnStart and strongly-typed options binding.
  • Anti-pattern detection checklist and migration steps for integrating into existing projects.

Quick Start

Create an IServiceCollection extension method for your layer, move all builder.Services.Add* calls into it, and call that method from Program.cs to centralize registrations and lifetimes.

Frequently Asked Questions about dependency-injection

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

FAQPage Schema
How do I choose between Singleton, Scoped, and Transient lifetimes in .NET dependency injection?

To organize .NET dependency injection registrations, create an IServiceCollection extension method for each application layer, move all builder.Services.Add* calls into it, and invoke that method from Program.cs to centralize and modularize service lifetimes.

How do I resolve multiple service implementations using .NET 8 keyed services?

Keyed services in .NET 8 dependency injection allow registering multiple implementations of an interface by assigning distinct keys, enabling precise resolution of specific service instances during constructor injection and decorator pattern applications.

How do I create a safe scope inside a singleton background service?

To create a safe scope inside a singleton background service, inject IServiceScopeFactory rather than a scoped dependency directly, preventing captive dependency issues by generating and disposing isolated scopes during execution.

How do I validate strongly-typed options binding on application startup?

Validate strongly-typed options binding during application startup by applying ValidateOnStart to your dependency injection configuration, ensuring invalid configurations fail immediately before the service container builds.