configuring-dependency-injection

Dependency Injection and GenericHost Usage

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-dependency-injection" with this command: npx skills add christian289/dotnet-with-claudecode/christian289-dotnet-with-claudecode-configuring-dependency-injection

Dependency Injection and GenericHost Usage

A guide on using Dependency Injection and GenericHost in .NET projects.

Core Principles

  • Implement dependency injection using Microsoft.Extensions.DependencyInjection

  • Use GenericHost (Microsoft.Extensions.Hosting) as the default

  • Apply service injection through Constructor Injection

Console and General Projects - Program.cs

// Program.cs using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;

// Configure DI using GenericHost var host = Host.CreateDefaultBuilder(args) .ConfigureServices((context, services) => { // Register services services.AddSingleton<IUserRepository, UserRepository>(); services.AddScoped<IUserService, UserService>(); services.AddTransient<IEmailService, EmailService>();

    // Register main application service
    services.AddSingleton&#x3C;App>();
})
.Build();

// Get service through ServiceProvider var app = host.Services.GetRequiredService<App>(); await app.RunAsync();

// Application class - Constructor Injection public sealed class App(IUserService userService, IEmailService emailService) { private readonly IUserService _userService = userService; private readonly IEmailService _emailService = emailService;

public async Task RunAsync()
{
    // Use injected services
    var users = await _userService.GetAllUsersAsync();

    foreach (var user in users)
    {
        await _emailService.SendWelcomeEmailAsync(user.Email);
    }
}

}

WPF Project - App.xaml.cs

// App.xaml.cs namespace MyApp;

using System.Windows; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting;

public partial class App : Application { private readonly IHost _host;

public App()
{
    // Create GenericHost and register services
    _host = Host.CreateDefaultBuilder()
        .ConfigureServices((context, services) =>
        {
            // Register services
            services.AddSingleton&#x3C;IUserRepository, UserRepository>();
            services.AddSingleton&#x3C;IUserService, UserService>();
            services.AddTransient&#x3C;IDialogService, DialogService>();

            // Register ViewModels
            services.AddTransient&#x3C;MainViewModel>();
            services.AddTransient&#x3C;SettingsViewModel>();

            // Register Views
            services.AddSingleton&#x3C;MainWindow>();
        })
        .Build();
}

protected override async void OnStartup(StartupEventArgs e)
{
    await _host.StartAsync();

    // Get MainWindow from ServiceProvider
    var mainWindow = _host.Services.GetRequiredService&#x3C;MainWindow>();
    mainWindow.Show();

    base.OnStartup(e);
}

protected override async void OnExit(ExitEventArgs e)
{
    using (_host)
    {
        await _host.StopAsync();
    }

    base.OnExit(e);
}

}

MainWindow.xaml.cs - Constructor Injection

// MainWindow.xaml.cs namespace MyApp;

using System.Windows;

public partial class MainWindow : Window { // ViewModel injection through Constructor Injection public MainWindow(MainViewModel viewModel) { InitializeComponent(); DataContext = viewModel; } }

ViewModel - Constructor Injection

// ViewModels/MainViewModel.cs namespace MyApp.ViewModels;

using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input;

public sealed partial class MainViewModel : ObservableObject { private readonly IUserService _userService; private readonly IDialogService _dialogService;

// Constructor Injection
public MainViewModel(IUserService userService, IDialogService dialogService)
{
    _userService = userService;
    _dialogService = dialogService;

    LoadDataAsync();
}

[ObservableProperty] private ObservableCollection&#x3C;User> _users = [];

[RelayCommand]
private async Task LoadDataAsync()
{
    try
    {
        var userList = await _userService.GetAllUsersAsync();
        Users = new ObservableCollection&#x3C;User>(userList);
    }
    catch (Exception ex)
    {
        await _dialogService.ShowErrorAsync("Error occurred", ex.Message);
    }
}

}

Service Lifetime Rules

Singleton

Creates only one instance throughout the application

  • Repository, global state management services

  • services.AddSingleton<IUserRepository, UserRepository>()

Scoped

Creates one instance per request (Scope)

  • DbContext, transaction-based services

  • services.AddScoped<IUserService, UserService>()

  • ⚠️ Generally not used in WPF (mainly used in Web applications)

Transient

Creates a new instance on every request

  • ViewModel, one-time services

  • services.AddTransient<MainViewModel>()

Direct Use of ServiceProvider (Not Recommended)

Anti-pattern: Service Locator

// Service Locator pattern (anti-pattern) public sealed class SomeClass { private readonly IServiceProvider _serviceProvider;

public SomeClass(IServiceProvider serviceProvider)
{
    _serviceProvider = serviceProvider;
}

public void DoSomething()
{
    // ⚠️ Not recommended: Using ServiceProvider directly
    var service = _serviceProvider.GetRequiredService&#x3C;IUserService>();
}

}

Recommended: Constructor Injection

// Correct way: Use Constructor Injection public sealed class SomeClass { private readonly IUserService _userService;

public SomeClass(IUserService userService)
{
    _userService = userService;
}

public void DoSomething()
{
    // ✅ Recommended: Use service injected through Constructor Injection
    _userService.GetAllUsersAsync();
}

}

Required NuGet Packages

<ItemGroup> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" /> </ItemGroup>

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

using-xaml-property-element-syntax

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

managing-styles-resourcedictionary

No summary provided by upstream source.

Repository SourceNeeds Review