Aspire4Wasm 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Aspire4Wasm --version 2.0.0                
NuGet\Install-Package Aspire4Wasm -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Aspire4Wasm" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Aspire4Wasm --version 2.0.0                
#r "nuget: Aspire4Wasm, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Aspire4Wasm as a Cake Addin
#addin nuget:?package=Aspire4Wasm&version=2.0.0

// Install Aspire4Wasm as a Cake Tool
#tool nuget:?package=Aspire4Wasm&version=2.0.0                

Aspire4Wasm

An easy way to pass service discovery information from a distributed application in Aspire down to your Blazor WebAssembly (client) applications. You can then add service discovery to the client app just like any other Aspire resource, and it just works!

Problem statement

.NET Aspire doesn't currently (as of early 2025) facilitate a Blazor WebAssembly (client) app discovering Aspire resources, even if the app has been added to the distributed application because Blazor WebAssembly apps run in the browser and are "standalone". This has been commented on here:

https://github.com/dotnet/aspire/issues/4785 https://stackoverflow.com/questions/78607828/how-to-add-aspire-to-blazor-webassembly-standalone-app-in-net-8

The expectation is that these apps will still be aware of the web APIs they're supposed to call and can store these in appsettings.json or appsettings.{environmentName}.json. This works fine, but if the endpoint changes, or if it differs in your development and production environments, you have to remember to manage those changes in your client app as well as your other resources. This library solves that problem by writing the service discovery information to the appsettings.{environmentName}.json file of your client app for you.

Quickstart

Install Aspire4Wasm in your AppHost project via the Nuget package. No need to install it on the client project. In your AppHost project's Program.cs file:

  1. Add the Web Apis you want your client to be able to call.
  2. Add your Blazor Server app then chain a call to AddWebAssemblyClient to add your client app.
  3. Chain a call to WithReference to point the client to each web API (you can repeat this for as many Web APIs as you need)

In your client's Program.cs file:

  1. Call AddServiceDiscovery
  2. Configure your HttpClients either globally or one at a time. In each client's BaseAddress property, use the name you gave to the resource in your AppHost.

See the example below:

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

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");
    });

Default behaviour

Using the default behaviour (in the example) your AppHost will write the service discovery information for all the referenced resources into the appsettings.{environmentName}.json file of your client app for you. It uses the following structure, recommended by the Aspire team:

{
  "Services": {
    "resourceName": {
      "https": [
        "https://localhost:7222"
      ],
      "http": [
        "http://localhost:5050"
      ]
    }
  }
}

Custom behaviours

If you want to serialize the service discovery information some other way in your WebAssembly application (for example, in a different JSON file, or in an XML file) you can do so in the AppHost Program.cs by creating a custom implementation of IServiceDiscoveryInfoSerializer and passing it to the call to AddWebAssemblyClient via the WebAssemblyProjectBuilderOptions class, like this:

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" options => {
        options.ServiceDiscoveryInfoSerializer = yourImplementation;
    })
    .WithReference(inventoryApi)
    .WithReference(billingApi);

builder.Build().Run();

Custom implementations of IServiceDiscoveryInfoSerializer

If you choose to make a custom implementation, you only need to override on method, ensuring that however you choose to serialize the information, Aspire will be able to read it in your client app:

public void SerializeServiceDiscoveryInfo(IResourceWithServiceDiscovery resource) { }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 115 1/11/2025
2.0.0 75 1/11/2025 2.0.0 is deprecated because it has critical bugs.
1.0.0 112 1/2/2025