Search code examples
azureblazor-webassemblydotnet-aspire

.NET Aspire doesn't replace http client Uri with actual address in blazor webassembly standalone app in .net 8


i referenced the blazor project to Aspire app host so this is my aspire app host code

using AnswerMe.Aspire.AppHost;

var builder = DistributedApplication.CreateBuilder(args);

var identityServerDb=builder.AddPostgres("identityserver", password: builder.CreateStablePassword("AnswerMeDB-password"))
    .WithDataVolume()
    .WithPgAdmin()
    .AddDatabase("IdentityServerDb");

var identityServerApi = builder.AddProject<Projects.IdentityServer_Api>("identityserverapi")
    .WithReference(identityServerDb);

var client = builder.AddProject<Projects.AnswerMe_Client>("answermeclient")
    .WithReference(identityServerApi);

builder. Build().Run();

and this is my blazor code to add HTTP client

builder.Services.AddHttpClient(nameof(HttpClients.IdentityServer),
    client => { client.BaseAddress = new("http://identityserverapi"); });

so as you can see in the result it doesn't replace the http://identityserverapi with the actual address

enter image description here

and also i found the builder.Configuration.GetConnectionString("identityserverapi"); doesn't work because we don't have appsettings.json in blazor wasm so I think Aspire doesn't work fo the blazor wasm


Solution

  • I had a similar issue. Support for Blazor WebAssembly projects isn't in Aspire yet. It's because Aspire has no way to pass the service discovery information to the client application except over HTTP.

    I made a Nuget package that passes the service discovery information from the AppHost to the client by writing it to the client's appsettings.json files. Hopefully one day the feature will be baked into Aspire.

    You use it like this:

    Example Program.cs in AppHost

    var builder = DistributedApplication.CreateBuilder(args);
    
    var inventoryApi = builder.AddProject<Projects.AspNetCoreWebApi>("inventoryapi");
    var billingApi = builder.AddProject<Projects.SomeOtherWebApi>("billingapi");
    
    builder.AddProject<Projects.Blazor>("blazorServer")
        .AddWebAssemblyClient<Projects.Blazor_Client>("blazorWasmClient")
        .WithReference(inventoryApi)
        .WithReference(billingApi);
    
    builder.Build().Run();
    

    Example Program.cs in your Blazor WebAssembly Client Install (on the WebAssembly client) the Microsoft.Extensions.ServiceDiscovery Nuget package to get the official Aspire service discovery functionality that is going to read your resource information from your app settings.

    builder.Services.AddServiceDiscovery();
    builder.Services.ConfigureHttpClientDefaults(static http =>
    {
        http.AddServiceDiscovery();
    });
    
    builder.Services.AddHttpClient<IInventoryService, InventoryService>(
        client =>
        {
            client.BaseAddress = new Uri("https+http://inventoryapi");
        });
    
        builder.Services.AddHttpClient<IBillingService, BillingService>(
        client =>
        {
            client.BaseAddress = new Uri("https+http://billingapi");
        });
    

    I hope it solves your problem.