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
<PackageReference Include="Extensions.Hosting.Wpf" Version="5.0.0" />
<PackageVersion Include="Extensions.Hosting.Wpf" Version="5.0.0" />
<PackageReference Include="Extensions.Hosting.Wpf" />
paket add Extensions.Hosting.Wpf --version 5.0.0
#r "nuget: Extensions.Hosting.Wpf, 5.0.0"
#:package Extensions.Hosting.Wpf@5.0.0
#addin nuget:?package=Extensions.Hosting.Wpf&version=5.0.0
#tool nuget:?package=Extensions.Hosting.Wpf&version=5.0.0
Microsoft.Extensions.Hosting.Wpf
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
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
OnStartuphas returned, the library identifies the dispatcher operation WPF'sApplicationconstructor queues for startup. If that ever cannot be identified, startup fails with anInvalidOperationExceptionrather 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
- HostingMinimal: Top-level statements with
Host.CreateApplicationBuilderand a code-only WPF application. - HostingSimple: Top-level statements with
App.xaml(EnableDefaultApplicationDefinitionset tofalseinstead ofStartupObject), XAML, and a view model locator. - HostingReactiveUI: An advanced example using NLog for logging, ReactiveUI as the MVVM framework, and the TrayIcon feature.
- HostingReactiveUISimpleInjector: Incorporates SimpleInjector alongside ReactiveUI. Demonstrates that you're not limited to
Microsoft.DependencyInjection. - HostingReactiveUISimpleInjectorAmbientScope: Demonstrates
AsyncScopedLifestylewith SimpleInjector for ambient-scoping and integrates Threading. - HostingReactiveUISimpleInjectorFlowingScope: Demonstrates
ScopedLifestyle.Flowingwith SimpleInjector for closure-scoping. Presents an alternative approach withoutViewModelLocatorHost.
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.
- Getting Started — Minimal setup guide
- ViewModelLocator Feature — Bind DataContext in XAML via DI
- Constructor Injection in App — Inject services into your App class
- Threading — Switch between UI and background threads
- TrayIcon — System tray icon support
- Bootstrap (3rd-party DI) — SimpleInjector, Autofac, and other DI containers
- Architecture & How It Works — Internal architecture and lifecycle
- API Reference — Complete API reference
- FAQ & Troubleshooting — Common questions and error solutions
| Product | Versions 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. |
-
.NETFramework 4.6.2
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
.NETFramework 4.8
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net10.0-windows7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net6.0-windows7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net7.0-windows7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net8.0-windows7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
-
net9.0-windows7.0
- Microsoft.Extensions.Hosting (>= 8.0.1)
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.