Aspire is now the talk of the town. You can build and orchestrate all the dependencies from one single place. In fact, it’s stack streamlined. Learn more about Aspire here.
This article discusses how to integrate .NET MAUI, Microsoft’s cross-platform UI framework, with Aspire.
Aspire supports .NET by default, but since .NET MAUI is designed for multi-targeting from a single project, it presents a minor issue. I’ll offer a workaround until it’s officially supported (coming soon).
For this, I’m using .NET 10 File-based Apps for simplicity. Aspire v9.5 supports this new feature. Consult this article to know more about C# File-apps.
Aspire’s standout feature is its service discoverability. Mobile apps rely heavily on APIs for data, and integrating .NET MAUI with Aspire offers a major advantage. No need to manage them separately.
The service discovery and telemetry features are organized as extension methods. These methods wire up in the startup pipeline. They are abstracted as a shared library. This library is added as a reference to both the API and the MAUI project.
Note: Please refrain from including any additional content, like model types, in this shared library. It is for Aspire’s use.
The HTTP setup in the .NET MAUI project utilizes Aspire’s service discovery feature with the resource name. Refer to the MauiProgram.cs source file for details. Solution linked at the end.
Aspire AppHost:
Aspire utilizes a different SDK, so it must be defined explicitly. I have included both the API and MAUI projects as references to the Aspire AppHost. They are defined in the pipeline and also listed as resources by their names. Additionally, while defining the MAUI project, I have included the API project as a resource dependency (project-wise, they’re independent).
Workaround:
To run the .NET MAUI project in Aspire, you need to set the TargetFramework parameter as a scoped environment value. In this instance, I am running the Windows target of the project. You can also set up other target frameworks as needed. Additionally, make sure that any dependent parameters are correctly set for the target platform. For Android, set the AdbTarget, and for iOS or Mac Catalyst, set the _DeviceName.
#:sdk Aspire.AppHost.Sdk@9.5.2
#:package Aspire.Hosting.AppHost@9.5.2
#:project ..\MauiApp1.Api\MauiApp1.Api.csproj
#:project ..\MauiApp1\MauiApp1.csproj
var builder = DistributedApplication.CreateBuilder(args);
// Configure builder
// Web API
var webapi = builder.AddProject<Projects.MauiApp1_Api>("webapi");
// .NET MAUI
builder.AddProject<Projects.MauiApp1>("mauiapp")
.WithEnvironment("TargetFramework", "net10.0-windows10.0.19041.0")
.WithReference(webapi);
var aspire = builder.Build();
aspire.Run();
A working solution is hosted on my .NET MAUI Samples GitHub repository in the src\NET_10\MauiAspire folder.
Under that, MauiApp1 is the solution folder. Now, navigate to the MauiApp1.Aspire subfolder, then execute.
dotnet Aspire.cs
This command will restore dependencies, build projects, start Aspire, and launch the .NET MAUI app. It is a simple app that performs an API request. Hit the Get Forecast button to see it in action.
You can launch the Aspire dashboard and view all the details. For this instance, it is running on port # 17164 on localhost.
Templates Support:
There is ongoing work to integrate boilerplate code into the All-in-One .NET MAUI app templates, including an opt-in parameter for Aspire functionality. This will be released at the earliest.
Update: Template support for Aspire Integration is now released. Check out the What’s New in the All-in-One .NET MAUI Templates Pack v7.11 article for details.
Happy coding. Stay connected as we continue to learn and share the experiences from this exciting journey of being a .NET developer.
2 replies on “Integrating .NET MAUI with Aspire: A Comprehensive Guide”
[…] .NET Aspire Integration […]
[…] recommended to consult the earlier article on Integrating .NET MAUI with Aspire for the approach used. In the template, Aspire AppHost is a project instead of a file-based […]