From wpf-dev-pack
Implements dependency injection using GenericHost in .NET console applications. Covers hosted services, background tasks, configuration, logging, service lifetimes, and anti-patterns like service locator.
How this skill is triggered — by the user, by Claude, or both
Slash command
/wpf-dev-pack:configuring-console-app-dihaikuThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
A guide for implementing dependency injection using GenericHost in .NET Console Application.
A guide for implementing dependency injection using GenericHost in .NET Console Application.
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.*" />
</ItemGroup>
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
services.AddSingleton<IMyService, MyService>();
services.AddSingleton<App>();
})
.Build();
var app = host.Services.GetRequiredService<App>();
await app.RunAsync();
namespace MyApp;
public sealed class App(IMyService myService)
{
private readonly IMyService _myService = myService;
public async Task RunAsync()
{
await _myService.DoWorkAsync();
}
}
| Lifetime | Description | Use When |
|---|---|---|
Singleton | Single instance for entire app | Stateless services |
Scoped | Single instance per request | DbContext |
Transient | New instance per injection | Lightweight services |
var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
config.AddJsonFile("appsettings.json", optional: true);
})
.ConfigureServices((context, services) =>
{
services.Configure<AppSettings>(
context.Configuration.GetSection("AppSettings"));
services.AddSingleton<App>();
})
.Build();
public sealed class App(ILogger<App> logger)
{
public Task RunAsync()
{
logger.LogInformation("Application started");
return Task.CompletedTask;
}
}
// ❌ Bad example
public sealed class BadService(IServiceProvider provider)
{
public void DoWork()
{
var service = provider.GetRequiredService<IMyService>();
}
}
// ✅ Good example
public sealed class GoodService(IMyService myService)
{
public void DoWork() { }
}
npx claudepluginhub christian289/dotnet-with-claudecode --plugin wpf-dev-packConfigures Dependency Injection using Microsoft.Extensions.DependencyInjection and GenericHost in .NET console and WPF apps. Use for DI container setup, service registration, and IoC patterns.
Registering or resolving services with MS DI. Keyed services, scopes, decoration, hosted services.
Covers .NET dependency injection patterns: service lifetimes, keyed services, decorator pattern, and common pitfalls. Useful when registering services or resolving lifetime issues.