Zum Inhalt springen
Docs Try Aspire
Docs Try

.NET MAUI integration

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

.NET MAUI logo

This article is the reference for the Aspire .NET MAUI integration. It documents the AppHost APIs you use to orchestrate .NET MAUI mobile and desktop applications in your AppHost project, and shows how to configure .NET MAUI apps to discover and connect to Aspire-managed services.

To use the .NET MAUI integration with Aspire, you need:

The .NET MAUI hosting integration is distributed via the 📦 Aspire.Hosting.Maui NuGet package. It enables you to orchestrate .NET MAUI applications in your AppHost project and register platform-specific device configurations.

To add the .NET MAUI hosting integration to your AppHost, install the 📦 Aspire.Hosting.Maui NuGet package:

Terminal
aspire add maui

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Maui@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Maui" Version="*" />

To orchestrate a .NET MAUI application in your AppHost, call AddMauiProject with the name and path to the .csproj file:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var weatherApi = builder.AddProject<Projects.WeatherApi>("webapi");
var mauiapp = builder.AddMauiProject("mauiapp", "../YourMauiApp/YourMauiApp.csproj");
// Add platform-specific device configurations here...
builder.Build().Run();

Each target platform requires a specific device configuration chained from the MAUI project resource:

Windows apps run directly on the host machine using localhost:

C# — AppHost.cs
mauiapp.AddWindowsDevice()
.WithReference(weatherApi);

On Windows and Mac Catalyst, local service connectivity works directly through localhost without requiring Dev Tunnels. iOS Simulator and Android Emulator require Dev Tunnels; the WithOtlpDevTunnel() method creates a Dev Tunnel specifically for OpenTelemetry Protocol (OTLP) traffic so telemetry from mobile targets reaches the Aspire dashboard.

The following example shows all platform configurations together in a single AppHost:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var weatherApi = builder.AddProject<Projects.WeatherApi>("webapi");
var publicDevTunnel = builder.AddDevTunnel("devtunnel-public")
.WithAnonymousAccess()
.WithReference(weatherApi.GetEndpoint("https"));
var mauiapp = builder.AddMauiProject("mauiapp", "../YourMauiApp/YourMauiApp.csproj");
mauiapp.AddWindowsDevice()
.WithReference(weatherApi);
mauiapp.AddMacCatalystDevice()
.WithReference(weatherApi);
mauiapp.AddiOSSimulator()
.WithOtlpDevTunnel()
.WithReference(weatherApi, publicDevTunnel);
mauiapp.AddAndroidEmulator()
.WithOtlpDevTunnel()
.WithReference(weatherApi, publicDevTunnel);
builder.Build().Run();

To configure your .NET MAUI app to discover and connect to Aspire-managed services:

  1. Create a MAUI Service Defaults project.
  2. Reference it from your MAUI app.
  3. Configure service defaults in your MAUI app.

The MAUI Service Defaults project contains shared configuration for your MAUI app, including service discovery, resilience, and telemetry setup:

.NET CLI — New service defaults
dotnet new maui-aspire-servicedefaults -n YourApp.MauiServiceDefaults

Add a reference to this project from your MAUI app:

.NET CLI — Add project reference
dotnet add YourMauiApp.csproj reference YourApp.MauiServiceDefaults/YourApp.MauiServiceDefaults.csproj

In your MauiProgram.cs, add service defaults and configure HTTP clients:

C# — MauiProgram.cs
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
builder.AddServiceDefaults();
builder.Services.AddHttpClient<WeatherApiClient>(client =>
{
client.BaseAddress = new Uri("https+http://webapi");
});
return builder.Build();
}
}

Create a strongly-typed HTTP client to consume your web service:

C# — WeatherApiClient.cs
public class WeatherApiClient
{
private readonly HttpClient _httpClient;
public WeatherApiClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<WeatherForecast[]?> GetWeatherForecastAsync(
CancellationToken cancellationToken = default)
{
return await _httpClient.GetFromJsonAsync<WeatherForecast[]>(
"/weatherforecast",
cancellationToken);
}
}
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC * 1.8);
}

Inject and use the client in your pages or view models:

C# — MainPage.cs
public partial class MainPage : ContentPage
{
private readonly WeatherApiClient _weatherClient;
public MainPage(WeatherApiClient weatherClient)
{
_weatherClient = weatherClient;
InitializeComponent();
}
private async void OnGetWeatherClicked(object sender, EventArgs e)
{
try
{
var forecasts = await _weatherClient.GetWeatherForecastAsync();
if (forecasts is not null)
{
ResultLabel.Text = $"Retrieved {forecasts.Length} forecasts";
}
}
catch (Exception ex)
{
ResultLabel.Text = $"Error: {ex.Message}";
}
}
}