WPF and Windows Forms with Aspire
WPF (Windows Presentation Foundation) and Windows Forms are .NET desktop UI frameworks for building Windows applications. Because they are standard .NET projects, you can register them in an Aspire AppHost alongside your backend services with the same AddDotnetProjectaddDotnetProjectAddDotnetProject API you use for web and worker services. Aspire then starts and monitors the desktop app as a local process resource when you run the AppHost during development.
Add WPF or Windows Forms to the AppHost
Section titled “Add WPF or Windows Forms to the AppHost”Desktop projects use the .NET / C# app integration, so no desktop-specific package is required.
-
Add the .NET hosting integration to your AppHost, if it isn’t already installed:
Terminal aspire add Aspire.Hosting.Dotnet -
Register the project in your AppHost by path and wire up any service references it needs:
apphost.mts import { createBuilder } from './.aspire/modules/aspire.mjs';const builder = await createBuilder();const api = await builder.addDotnetProject('api', '../ApiService/ApiService.csproj');await builder.addDotnetProject('desktop', '../MyWpfApp/MyWpfApp.csproj').withReference(api).waitFor(api);await builder.build().run();AppHost.cs var builder = DistributedApplication.CreateBuilder(args);var api = builder.AddDotnetProject("api", "../ApiService/ApiService.csproj");builder.AddDotnetProject("desktop", "../MyWpfApp/MyWpfApp.csproj").WithReference(api).WaitFor(api);builder.Build().Run();apphost.go api := builder.AddDotnetProject("api", "../ApiService/ApiService.csproj")builder.AddDotnetProject("desktop", "../MyWpfApp/MyWpfApp.csproj").WithReference(api).WaitFor(api)When you run the AppHost, the desktop application starts automatically as a process resource alongside all other registered services.
-
Add service discovery to the desktop project so it can resolve Aspire-managed service addresses at runtime. Reference the
Aspire.ServiceDefaultsproject (or installMicrosoft.Extensions.ServiceDiscoverydirectly), then configure the generic host in your startup code.For WPF, override
OnStartupinApp.xaml.cs:App.xaml.cs (WPF) public partial class App : Application{public IServiceProvider Services { get; private set; } = default!;protected override void OnStartup(StartupEventArgs e){base.OnStartup(e);var host = Host.CreateDefaultBuilder().ConfigureServices(services =>{services.AddServiceDiscovery();services.AddHttpClient<ApiClient>(client =>{client.BaseAddress = new Uri("https+http://api");}).AddServiceDiscovery();}).Build();Services = host.Services;host.Start();}}For Windows Forms, configure the host in
Program.cs:Program.cs (Windows Forms) var host = Host.CreateDefaultBuilder(args).ConfigureServices(services =>{services.AddServiceDiscovery();services.AddHttpClient<ApiClient>(client =>{client.BaseAddress = new Uri("https+http://api");}).AddServiceDiscovery();services.AddTransient<MainForm>();}).Build();Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(host.Services.GetRequiredService<MainForm>());
Dev-only orchestration
Section titled “Dev-only orchestration”A common pattern is to register the desktop app only during local development and skip it in CI or staging environments. Check the AppHost environment to add the resource conditionally:
import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();const env = await builder.environment.get();
const postgres = await builder.addPostgres('postgres');const db = await postgres.addDatabase('mydb');
const api = await builder .addDotnetProject('api', '../ApiService/ApiService.csproj') .withReference(db);
if (await env.isDevelopment()) { await builder .addDotnetProject('desktop', '../MyWpfApp/MyWpfApp.csproj') .withReference(api) .waitFor(api);}
await builder.build().run();var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .AddDatabase("mydb");
var api = builder.AddDotnetProject("api", "../ApiService/ApiService.csproj") .WithReference(postgres);
if (builder.Environment.IsDevelopment()){ builder.AddDotnetProject("desktop", "../MyWpfApp/MyWpfApp.csproj") .WithReference(api) .WaitFor(api);}
builder.Build().Run();Limitations
Section titled “Limitations”- Windows only: WPF and Windows Forms require Windows. On macOS or Linux, the desktop resource will fail to start because the .NET WPF/WinForms runtime is unavailable on those platforms.
- No deployment support:
aspire deploydoes not produce deployment artifacts for desktop apps. They are treated as development-time resources only. - Dashboard lifecycle: Desktop apps start automatically when the AppHost runs. Use the dashboard’s Stop, Start, Restart, and Rebuild commands to control the desktop app process.
Comparison with .NET MAUI
Section titled “Comparison with .NET MAUI”| Feature | WPF / WinForms | .NET MAUI |
|---|---|---|
| Aspire hosting package | Aspire.Hosting.Dotnet (AddDotnetProject) | Aspire.Hosting.Maui |
| Platform support | Windows only | Windows, macOS, iOS, Android |
| Dev Tunnels support | Not applicable | Built-in for iOS/Android |
| Production deployment with Aspire | Not supported | Not supported |
For cross-platform desktop and mobile scenarios, consider .NET MAUI integration.