0

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.

1 Answer 1

0

The issue seems to be related to older packages I had installed, particularly

<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.0.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.14.0-rc.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0-rc.1" />

Removing these creates a new error, but will raise separately

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.