Extensions.Hosting.Wpf 5.0.0

dotnet add package Extensions.Hosting.Wpf --version 5.0.0
                    
NuGet\Install-Package Extensions.Hosting.Wpf -Version 5.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Extensions.Hosting.Wpf" Version="5.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Extensions.Hosting.Wpf" Version="5.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Extensions.Hosting.Wpf" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Extensions.Hosting.Wpf --version 5.0.0
                    
#r "nuget: Extensions.Hosting.Wpf, 5.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Extensions.Hosting.Wpf@5.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Extensions.Hosting.Wpf&version=5.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Extensions.Hosting.Wpf&version=5.0.0
                    
Install as a Cake Tool

Microsoft.Extensions.Hosting.Wpf

Nuget Nuget GitHub codecov

Unofficial implementation of Microsoft.Extensions.Hosting for WPF. It is inspired by Dapplo and this extensions is focused only on WPF and doesn't have Plugins, SingleInstance etc features like Dapplo. It's main feature is to provide the ability to bind DataContext with ViewModels directly in XAML where the ViewModel gets resolved by DI. This library also has few extensions packages to add features like tray icon, thread switching between main thread and threadpool thread, 3rd party DI support.

Changelog | Wiki

NuGet Packages

Name Description Latest version
Extensions.Hosting.Wpf The main library, with Microsoft.Extensions.Hosting for WPF support. NuGet
Extensions.Hosting.Wpf.TrayIcon An extension enabling tray icon support. NuGet
Extensions.Hosting.Wpf.Threading An extension enabling thread switching between Main UI Thread and ThreadPool Thread via Microsoft.VisualStudio.Threading. NuGet
Extensions.Hosting.Wpf.Bootstrap An extension enabling easier bootstrap for 3rd-party DI containers. Requires an explicit Extensions.Hosting.Wpf reference as of 4.0.0. NuGet

Quick Start

1. Add IApplicationInitializeComponent to App.xaml.cs

public partial class App : Application, IApplicationInitializeComponent
{
}

2. Create Program.cs

Use Host.CreateApplicationBuilder from Microsoft.Extensions.Hosting 8.0.1. Everything is registered on builder.Services before calling Build(), so the same calls work with HostApplicationBuilder, ASP.NET Core's WebApplicationBuilder (on every supported .NET version, although it implements IHostApplicationBuilder only from .NET 8), any other IHostApplicationBuilder, and inside IHostBuilder.ConfigureServices. As with Microsoft's Generic Host, the built application is still an IHost.

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Wpf;

public class Program
{
    public static void Main(string[] args)
    {
        var builder = Host.CreateApplicationBuilder(args);
        builder.Services.AddWpf<App>();
        builder.Services.AddWpfLifetime();

        using IHost host = builder.Build();
        host.Run();
    }
}

The same setup works with top-level statements: in a code-only WPF application (see HostingMinimal), or in one with App.xaml when WPF's generated Main is turned off as shown in step 3.

var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWpf<App>();
builder.Services.AddWpfLifetime();

using var host = builder.Build();
host.Run();

If your application implements IApplicationInitialize, add builder.Services.AddWpfInitialization<App>(). For a view model locator, use this instead:

builder.Services.AddWpfViewModelLocator<App, IViewModelLocator>(
    provider => new ViewModelLocator(provider));

Application initialization and the locator factory run on the WPF UI thread, after InitializeComponent() and before Application.Run(). The factory receives the built host's service provider. Use either initialization extension once per application; the locator extension also calls Initialize(). If AddWpf<App>() is missing, host startup fails with an InvalidOperationException.

WPF is created and started once every hosted service has started, the way ASP.NET Core starts serving requests. If any hosted service fails to start, WPF is never created. Another hosted service that needs the UI (the dispatcher, JoinableTaskFactory.SwitchToMainThreadAsync, ...) should use it from IHostApplicationLifetime.ApplicationStarted, not from its own StartAsync.

Started last, WPF is also stopped first: before any hosted service's StopAsync, the host waits until WPF has fully exited (OnExit, tray icon disposal), up to HostOptions.ShutdownTimeout. Application cleanup can therefore still use other hosted services and every registered service.

Known limitations:

  • Waiting for WPF is bounded by HostOptions.ShutdownTimeout. Application code that blocks longer (in initialization or cleanup) may still run after the host has started disposing services.
  • To know when OnStartup has returned, the library identifies the dispatcher operation WPF's Application constructor queues for startup. If that ever cannot be identified, startup fails with an InvalidOperationException rather than reporting the wrong moment.

AddWpfLifetime() is optional. With any host lifetime, AddWpf stops the host when the WPF application exits. WpfLifetime replaces the default ConsoleLifetime with one suited to a windowed application: no Ctrl+C handling or console shutdown hints, WPF status messages (turn them off with AddWpfLifetime(options => options.SuppressStatusMessages = true)), and a graceful host shutdown when the process exits.

With IHostBuilder, use the same IServiceCollection extensions or the Use* shortcuts:

using IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services => services.AddWpf<App>())
    .UseWpfLifetime()
    .UseWpfViewModelLocator<App, IViewModelLocator>(provider => new ViewModelLocator(provider))
    .Build();
host.Run();

The post-build IHost extensions (host.UseWpfInitialization<App>(), host.UseWpfViewModelLocator<App, ...>(...)) remain available for setups that must run after the host is built, for example when a third-party container is bootstrapped from the built host. Configure initialization in only one place: combining the pre-build and post-build extensions throws.

3. Update .csproj

WPF generates a Main for App.xaml that creates and runs the application itself. Make your Program the entry point instead:

<StartupObject>[Namespace].Program</StartupObject>

Alternatively, stop WPF from generating that Main. No StartupObject is needed then, and Program.cs can use top-level statements:

<EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>

App.xaml is then compiled like any other XAML file: InitializeComponent() (with StartupUri and the application resources) is still generated, only Main is not.

For the full guide including ViewModel Locator, constructor injection, and more, see the Wiki.

Samples

  1. HostingMinimal: Top-level statements with Host.CreateApplicationBuilder and a code-only WPF application.
  2. HostingSimple: Top-level statements with App.xaml (EnableDefaultApplicationDefinition set to false instead of StartupObject), XAML, and a view model locator.
  3. HostingReactiveUI: An advanced example using NLog for logging, ReactiveUI as the MVVM framework, and the TrayIcon feature.
  4. HostingReactiveUISimpleInjector: Incorporates SimpleInjector alongside ReactiveUI. Demonstrates that you're not limited to Microsoft.DependencyInjection.
  5. HostingReactiveUISimpleInjectorAmbientScope: Demonstrates AsyncScopedLifestyle with SimpleInjector for ambient-scoping and integrates Threading.
  6. HostingReactiveUISimpleInjectorFlowingScope: Demonstrates ScopedLifestyle.Flowing with SimpleInjector for closure-scoping. Presents an alternative approach without ViewModelLocatorHost.

Documentation

Full documentation is available in the Wiki:

Extensions.Hosting.Wpf.Bootstrap no longer includes Extensions.Hosting.Wpf transitively starting with 4.0.0, so bootstrap consumers must install both packages explicitly.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0-windows was computed.  net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net9.0-windows7.0 is compatible.  net10.0-windows was computed.  net10.0-windows7.0 is compatible. 
.NET Framework net462 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Extensions.Hosting.Wpf:

Package Downloads
Extensions.Hosting.Wpf.Threading

Adds Microsoft.VisualStudio.Threading support for Extensions.Hosting.Wpf

Extensions.Hosting.Wpf.TrayIcon

Adds tray icon for Extensions.Hosting.Wpf

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
5.0.0 93 10/19/2026
4.1.1 179 10/13/2026
4.1.0 137 10/13/2026
4.0.0 253 8/17/2026
3.4.0 2,289 11/23/2023
3.3.0 901 3/8/2023
3.2.0 786 2/7/2023
3.1.0 826 1/26/2023
3.0.0 1,294 6/24/2022
2.0.0 1,076 6/8/2022
1.0.0 857 6/5/2022