This repository demonstrates the integration between .NET MAUI and .NET Aspire, showcasing how Aspire simplifies connecting mobile and desktop applications to local web services during development.
.NET Aspire provides a consistent, opinionated set of tools and patterns for building and running distributed applications. It's designed to improve the experience of building cloud-native applications by providing:
- Orchestration: Simplified management of multiple services and dependencies
- Components: Pre-built integrations for common services and platforms
- Tooling: Developer dashboard for monitoring and managing services
- Service discovery: Automatic configuration for service-to-service communication
Integrating Aspire with your .NET MAUI applications provides several key benefits:
- Simplified configuration: Eliminate complex platform-specific networking configuration. No need to manually handle
10.0.2.2for Android or deal with certificate validation issues. - Automatic service discovery: Your MAUI app automatically discovers and connects to local services without hardcoded URLs.
- Development tunnels integration: Built-in support for Dev Tunnels on iOS and Android, making it easy to connect mobile emulators and simulators to local services.
- Consistent experience: Use the same patterns and tools across your entire application stack.
- Observable services: Monitor your services through the Aspire dashboard during development.
Aspire 13 and the Aspire.Hosting.Maui (preview) package has full support for Windows, Mac Catalyst, iOS and Android on physical devices as well as emulators and Simulators.
To use this sample or integrate Aspire with your own .NET MAUI project, you need:
- .NET 10 SDK or later
- .NET MAUI workload installed
This repository contains a complete working example with:
- SampleMauiApp: A .NET MAUI application that consumes a weather API
- SampleWebApi: An ASP.NET Core Web API providing weather data
- MauiApp.ServiceDefaults: Service defaults configuration for the MAUI app
- MauiAspire.ServiceDefaults: Service defaults configuration for the web API
- MauiAspire.AppHost: The Aspire App Host that orchestrates all services
- Clone this repository
- Open the solution in your preferred IDE or editor
- Run the App Host project:
- Visual Studio: Set
MauiAspire.AppHostas the startup project and press F5 - VS Code: Open the AppHost folder and run/debug the project
- Command line: Navigate to the
MauiAspire.AppHostdirectory and rundotnet runoraspire run
- Visual Studio: Set
- The Aspire dashboard will open, showing all registered services
- Your MAUI app will launch and automatically connect to the weather API
Follow these steps to add Aspire integration to your own .NET MAUI application:
Ensure your .NET MAUI app is using:
Microsoft.Maui.Controls.NET 10 RC2 or later- Target framework:
net10.0or later
The MAUI Service Defaults project contains shared configuration for service discovery, telemetry, and resilience patterns.
dotnet new maui-aspire-servicedefaults -n YourApp.MauiServiceDefaultsAdd a reference from your MAUI app to the Service Defaults project:
dotnet add YourMauiApp.csproj reference YourApp.MauiServiceDefaults/YourApp.MauiServiceDefaults.csprojTip
To enable detailed metrics and tracing from the .NET MAUI SDK, open the Extensions.cs file in your MAUI Service Defaults project and uncomment the lines that add Microsoft.Maui as a meter and tracing source. Note that this may generate a significant amount of telemetry data.
The App Host project orchestrates all your application services.
dotnet new aspire-apphost -n YourApp.AppHostAdd references to your MAUI app and web service projects:
dotnet add YourApp.AppHost.csproj reference YourWebService/YourWebService.csprojNote
Unlike traditional project references, MAUI projects are added using the AddMauiProject method in code (see next step), not as a project reference in the .csproj file.
In your App Host project's AppHost.cs or Program.cs, register your services and MAUI app:
var builder = DistributedApplication.CreateBuilder(args);
// Register your web service
var webapi = builder.AddProject<Projects.SampleWebApi>("webapi");
// Register your MAUI app using the path to the .csproj file
var mauiapp = builder.AddMauiProject("mauiapp", "../YourMauiApp/YourMauiApp.csproj");
// Add Windows device (uses localhost directly)
mauiapp.AddWindowsDevice()
.WithReference(webapi);
// Add Mac Catalyst device (uses localhost directly)
mauiapp.AddMacCatalystDevice()
.WithReference(webapi);
// Coming soon: iOS and Android support
// mauiapp.AddiOSSimulator()
// .WithOtlpDevTunnel()
// .WithReference(webapi, publicDevTunnel);
builder.Build().Run();In your MAUI app's MauiProgram.cs, add service defaults and configure HTTP clients:
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");
});
// Add service defaults - this enables service discovery and telemetry
builder.AddServiceDefaults();
// Configure HTTP client with service discovery
builder.Services.AddHttpClient<WeatherApiClient>(client =>
{
// Service name must match the name used in App Host
// The https+http:// scheme prefers HTTPS but falls back to HTTP
client.BaseAddress = new Uri("https+http://webapi");
});
return builder.Build();
}
}Create a typed client to consume your web service:
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);
}
}Inject and use the client in your pages or view models:
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 != null)
{
// Display the weather data
ResultLabel.Text = $"Retrieved {forecasts.Length} forecasts";
}
}
catch (Exception ex)
{
ResultLabel.Text = $"Error: {ex.Message}";
}
}
}Run the App Host project using your preferred method:
- Visual Studio: Set the App Host project as the startup project and press F5
- VS Code: Open the AppHost folder and run/debug the project
- Command line: Navigate to the App Host directory and run
dotnet runoraspire run
When running:
- The Aspire dashboard will open at
https://localhost:[port] - Your MAUI app will launch and connect to services automatically
- Monitor logs, traces, and metrics in the Aspire dashboard
On Windows and Mac Catalyst, the Aspire integration works seamlessly using localhost. Service discovery automatically provides the correct URLs for connecting to local services.
Support for iOS and Android is coming in a future Aspire release. When available, Dev Tunnels will be automatically configured to enable connectivity between mobile emulators/devices and services running on your development machine.
This sample showcases:
- ✅ Complete project structure for Aspire + MAUI
- ✅ Service registration and discovery
- ✅ HTTP client configuration with service discovery
- ✅ Telemetry and monitoring through the Aspire dashboard
- ✅ Windows and Mac Catalyst platform support
- ✅ Error handling and resilience patterns
If your MAUI app can't connect to services:
- Verify
AddServiceDefaults()is called inMauiProgram.cs - Ensure the service name in your HTTP client matches the App Host registration
- Check that you're using the
https+http://scheme - Verify all projects build successfully
If the App Host fails to start:
- Ensure all project paths in
AppHost.csare correct - Verify .NET 10 SDK and Aspire workload are installed
- Check the App Host console output for error messages
- Ensure your web service projects target a compatible framework
If you see errors about missing service defaults:
- Verify the Service Defaults project reference in your MAUI
.csproj - Ensure the Service Defaults project builds successfully
- Clean and rebuild the solution
- Official .NET Aspire Documentation
- .NET MAUI Documentation
- Service Discovery in Aspire
- Aspire App Host Overview
Contributions are welcome! Please feel free to submit issues or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.