Search code examples
blazorblazor-webassemblydotnet-aspire

how to add Aspire to blazor webassembly standalone app in .net 8


when i create new blazor webassembly project in visual studio i check the enlist in .net aspire orchestration box and it will be add to my project enter image description here

but when I started my project I couldn't see any blazor project running on Aspire

enter image description here

I have a few project with APIs and single blazor webassembly project and I want to add Aspire to them but it doesn't run blazor webassembly part

and also I tried to add it manually so my API was added but for blazor webassembly I getting error enter image description here


Solution

  • In the host project add a project reference to the WebAssembly application.

    In the program.cs of the Host project BlazorApp38.AppHost add. enter image description here Select the WebAssembly project.

    enter image description here

    The source generator should then kick in. Projects.BlazorApp38 should be defined.

    In program.cs cs simply add

    builder.AddProject<Projects.BlazorApp38>("webapp");
    
    var builder = DistributedApplication.CreateBuilder(args);
    builder.AddProject<Projects.BlazorApp38>("webapp");
    builder.Build().Run();
    

    It should look like this.

    Remember this app will be running in the clients browser..

    enter image description here

    I recommend using a hosted app with something like yarp to proxy to the internal api's of the distributed app, that's the point of using a framework like Aspire right?. The client can make secure calls to its host which uses a reverse proxy to resolve the internal aspire endpoint(s).