microsoft-extensions

Guides correct use of Microsoft.Extensions for dependency injection, configuration, logging, options, and HttpClientFactory in .NET apps.

Updated May 2, 2026
One-click install
npx skills add https://github.com/hdeshev/pi-config --skill microsoft-extensions-hdeshev
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: microsoft-extensions
Source: https://github.com/hdeshev/pi-config/tree/main/agent/skills/microsoft-extensions
Command: npx skills add https://github.com/hdeshev/pi-config --skill microsoft-extensions-hdeshev

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? .NET developers frequently misuse the Microsoft.Extensions stack, leading to captive dependencies, socket exhaustion from ad-hoc HttpClient usage, unvalidated configuration that fails at runtime, and unstructured logging that defeats observability. ## Core Features & Use Cases - DI and Generic Host Guidance: Enforces constructor injection, correct service lifetimes, keyed services, and predictable service registration for console apps, workers, and ASP.NET Core apps. - Configuration and Options Patterns: Promotes strongly typed settings, startup validation with ValidateOnStart, and correct selection between IOptions, IOptionsSnapshot, and IOptionsMonitor. - HttpClientFactory and Logging Standards: Directs use of typed and named clients with resilience handlers, structured message templates, and source-generated logging. - Use Case: When adding a background worker to a .NET console app, apply this Skill to wire the Generic Host, register hosted services with proper scopes, and validate configuration at startup. ## Quick Start Review my .NET service registration and hosted service setup and fix any dependency injection or configuration anti-patterns using Microsoft.Extensions best practices.

Frequently Asked Questions about microsoft-extensions

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

FAQPage Schema
How do I register services with dependency injection in .NET?▼

Register services on IServiceCollection using AddSingleton, AddScoped, or AddTransient, mapping interfaces to implementations like services.AddScoped<IOrderService, OrderService>(). Encapsulate related registrations in extension methods and compose them at the application edge in Program.cs.

How do I use IHttpClientFactory instead of new HttpClient?▼

Register typed clients with services.AddHttpClient<MyClient>() and inject HttpClient via the constructor. This avoids socket exhaustion from per-call instances and DNS staleness from static singletons, and supports resilience handlers like retries and circuit breakers.

What is the difference between IOptions, IOptionsSnapshot, and IOptionsMonitor?▼

IOptions is a singleton that never reloads after startup. IOptionsSnapshot is scoped and reloads per request, suiting web apps. IOptionsMonitor is a singleton with live reload notifications, ideal for long-running services needing current configuration values.

Why does my singleton service break when injecting DbContext?▼

This is a captive dependency: DbContext is scoped by default, so a singleton capturing it causes threading issues and stale data. Inject IServiceScopeFactory instead and create a scope per unit of work to resolve the DbContext correctly.

How do I validate configuration at startup in .NET?▼

Use services.AddOptions<T>().BindConfiguration("Section").ValidateDataAnnotations().ValidateOnStart() to fail fast on invalid settings. You can also add custom Validate callbacks for rules like URI format checks, preventing runtime failures later.

When should I not use the Generic Host in a .NET app?▼

Skip the Generic Host for trivial scripts or apps needing no configuration, DI, logging, or hosted services. Also avoid building mini-frameworks over Microsoft.Extensions unless the codebase genuinely requires reusable composition primitives.