Lewati ke konten
Docs Try Aspire
Docs Try

WPF and Windows Forms with Aspire

Konten ini belum tersedia dalam bahasa Anda.

.NET logo

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 using the same AddProject 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.

No additional NuGet package is required. Add the desktop project reference to the AppHost and call AddProject to register it.

  1. Add a project reference from your AppHost .csproj to the WPF or Windows Forms project:

    XML — AppHost.csproj
    <ItemGroup>
    <ProjectReference Include="..\MyWpfApp\MyWpfApp.csproj" />
    </ItemGroup>
  2. Register the project in your AppHost and wire up any service references it needs:

    C# — AppHost.cs
    var builder = DistributedApplication.CreateBuilder(args);
    var api = builder.AddProject<Projects.ApiService>("api");
    builder.AddProject<Projects.MyWpfApp>("desktop")
    .WithReference(api)
    .WaitFor(api);
    // After adding all resources, run the app...
    builder.Build().Run();

    When you run the AppHost, the desktop application starts automatically as a process resource alongside all other registered services.

  3. Add service discovery to the desktop project so it can resolve Aspire-managed service addresses at runtime. Reference the Aspire.ServiceDefaults project (or install Microsoft.Extensions.ServiceDiscovery directly), then configure the generic host in your startup code.

    For WPF, override OnStartup in App.xaml.cs:

    C# — 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:

    C# — 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>());

A common pattern is to register the desktop app only during local development and skip it in CI or staging environments. Use builder.Environment.IsDevelopment() to add the resource conditionally:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres")
.AddDatabase("mydb");
var api = builder.AddProject<Projects.ApiService>("api")
.WithReference(postgres);
if (builder.Environment.IsDevelopment())
{
builder.AddProject<Projects.MyWpfApp>("desktop")
.WithReference(api)
.WaitFor(api);
}
// After adding all resources, run the app...
builder.Build().Run();
  • 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 deploy does 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. They cannot be started or stopped on demand from the Aspire dashboard the same way containerized services can.
FeatureWPF / WinForms.NET MAUI
Aspire hosting packageNone — uses standard AddProjectAspire.Hosting.Maui
Platform supportWindows onlyWindows, macOS, iOS, Android
Dev Tunnels supportNot applicableBuilt-in for iOS/Android
Production deployment with AspireNot supportedNot supported

For cross-platform desktop and mobile scenarios, consider .NET MAUI integration.