configuring-console-app-di

Console Application DI Pattern

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "configuring-console-app-di" with this command: npx skills add christian289/dotnet-with-claudecode/christian289-dotnet-with-claudecode-configuring-console-app-di

Console Application DI Pattern

A guide for implementing dependency injection using GenericHost in .NET Console Application.

  1. Required NuGet Package

<ItemGroup> <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.*" /> </ItemGroup>

  1. Basic Implementation

2.1 Program.cs

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();

2.2 App.cs

namespace MyApp;

public sealed class App(IMyService myService) { private readonly IMyService _myService = myService;

public async Task RunAsync()
{
    await _myService.DoWorkAsync();
}

}

  1. Service Lifetime

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

  1. Configuration Integration

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();

  1. Logging Integration

public sealed class App(ILogger<App> logger) { public Task RunAsync() { logger.LogInformation("Application started"); return Task.CompletedTask; } }

  1. Important Notes

⚠️ Avoid Service Locator Pattern

// ❌ 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() { } }

⚠️ Captive Dependency

  • Singleton should not inject Scoped/Transient dependencies
  1. References
  • .NET Generic Host

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

converting-html-css-to-wpf-xaml

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

publishing-wpf-apps

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

managing-styles-resourcedictionary

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

using-xaml-property-element-syntax

No summary provided by upstream source.

Repository SourceNeeds Review