microsoft-extensions-dependency-injection

Organizes .NET service registrations into composable extension methods with correct lifetime management.

Updated Mar 8, 2026
One-click install
npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill microsoft-extensions-dependency-injection-agibuild
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: microsoft-extensions-dependency-injection
Source: https://github.com/AGIBuild/dotnet.CI.template/tree/main/.cursor/skills/microsoft-extensions-dependency-injection
Command: npx skills add https://github.com/AGIBuild/dotnet.CI.template --skill microsoft-extensions-dependency-injection-agibuild

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? ASP.NET Core applications often accumulate hundreds of unorganized service registrations in Program.cs, making configuration hard to navigate, reuse in tests, and maintain across teams. This Skill provides patterns for structuring dependency injection registrations into cohesive, testable extension methods. ## Core Features & Use Cases - Extension Method Composition: Group related registrations into Add{Feature}Services() methods placed next to the services they register, keeping Program.cs clean and chainable. - Lifetime Management Guidance: Choose correctly between Singleton, Scoped, and Transient lifetimes, and avoid common mistakes like injecting scoped services into singletons. - Scope Handling in Background Work: Create manual scopes in BackgroundService and Akka.NET actors where no automatic request scope exists. - Use Case: When building an order-processing API, register AddOrderServices(), AddEmailServices(), and AddPaymentServices() in Program.cs, then reuse those same extensions in WebApplicationFactory tests while overriding only external dependencies like payment processors. ## Quick Start Refactor my Program.cs service registrations into feature-based IServiceCollection extension methods with correct lifetimes.

Frequently Asked Questions about microsoft-extensions-dependency-injection

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

FAQPage Schema
How do I organize service registrations in ASP.NET Core?▼

Group related registrations into IServiceCollection extension methods named Add{Feature}Services(), placed next to the services they register. Program.cs then chains these methods, keeping startup code short and boundaries between subsystems clear.

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

Singleton suits stateless, thread-safe services like caches and template renderers. Scoped fits per-request state such as DbContext and repositories. Transient works for cheap, short-lived objects like validators.

How do I reuse DI configuration in integration tests?▼

Call the same production Add* extension methods inside WebApplicationFactory's ConfigureServices, then use RemoveAll<T>() to swap external dependencies like email senders or payment processors with test doubles.

Why does injecting a scoped service into a singleton fail?▼

A singleton captures the scoped instance at startup, so a DbContext would live forever and serve stale data. Inject IServiceProvider instead and create a scope per unit of work to resolve scoped services correctly.

How do I use scoped services in Akka.NET actors?▼

Actors have no automatic DI scope, so inject IServiceProvider and create a scope per message with CreateScope(). Resolve scoped services like repositories inside that scope so each message gets a fresh DbContext.