I am trying to use the new .NET Maui Aspire integration to make my development life easier and I cannot get the service discovery to work where the app detects the API's URL. I'm following this https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/aspire-integration?view=net-maui-10.0
This is my AppHost.cs file:
var builder = DistributedApplication.CreateBuilder(args);
var api = builder.AddProject("webapi", @"../My.Api/My.Api.csproj");
var publicDevTunnel = builder.AddDevTunnel("devtunnel-public")
.WithAnonymousAccess() // All ports on this tunnel default to allowing anonymous access
.WithReference(api.GetEndpoint("https"));
var mauiApp = builder
.AddMauiProject("MyApp", @"../My.App/My.App.csproj");
// Add Android emulator with default emulator (uses running or default emulator)
mauiApp
.AddAndroidEmulator()
.WithOtlpDevTunnel() // Required for OpenTelemetry data collection
.WithReference(api, publicDevTunnel);
builder.Build().Run();
And in my app, I have:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts => { fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); });
// Add service defaults & Aspire components.
builder.AddServiceDefaults();
// HttpClients
builder.Services.AddHttpClient<DeviceApiClient>(client =>
{
// Service name matches the name used in App Host
client.BaseAddress = new Uri("https+http://webapi");
});
builder.Services.AddHttpClient<UserApiClient>(client =>
{
// Service name matches the name used in App Host
client.BaseAddress = new Uri("https+http://webapi");
});
builder.Services.AddAuthorizationCore();
builder.Services.AddMauiBlazorWebView();
builder.Services.AddMudServices();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
return builder.Build();
}
The Aspire solution runs as expected and I can start the Android app from the Aspire dashboard, but the app is not resolving the Web API url.
public class DeviceApiClient(HttpClient httpClient)
{
public async Task CheckDeviceRegistration(SettingsService.DeviceVerificationRequest request)
{
var result = await httpClient.PostAsJsonAsync("api/facility/devices/verify", request);
result.EnsureSuccessStatusCode();
}
}
In this call the httpClient's base address is resolving to https://webapi/api/facility/devices/verify.
I have two ServiceDefaults, one for the Maui app and one for the Web API.
I realise this is in preview, so not 100% completed yet, but I have seen others be able to do this, so I'm hoping it's something silly I've misconfigured.