I literally did not add any of my own code, using only templates provided with VS2022, .NET 8.
There are the steps I took:
Step 1
Create Blazor web app "DemoServer", targeting .NET 8 using standard template "Blazor Web App".
Rendering mode in App.razor:
<head> <HeadOutlet @rendermode="InteractiveServer" /> </head> <body> <Routes @rendermode="InteractiveServer" /> <script src="_framework/blazor.web.js"></script> </body> Step 2
Create a web assembly project WasmFileEditor to serve wasm component. Add another project to the solution, based on the template "Blazor Webassembly App Empty".
Add Razor component FileEditor.razor:
<h3>File Editor Wasm Component</h3> @code { } Add breakpoint in Program.cs, execute the project. Breakpoint is hit.
Step 3
- Add
WasmProjectas a project reference toDemoServer. - Add nuget package
Microsoft.AspNetCore.Components.WebAssembly.ServertoWasmProject
Make following changes in WasmProject's Program.cs:
builder.Services.AddRazorComponents() .AddInteractiveServerComponents() .AddInteractiveWebAssemblyComponents(); app.MapRazorComponents<App>() .AddInteractiveServerRenderMode() .AddInteractiveWebAssemblyRenderMode() .AddAdditionalAssemblies(new[] { typeof(WasmFileEditor.FileEditor).Assembly }); <Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="new[] { typeof(WasmFileEditor.FileEditor).Assembly}"> <Found Context="routeData"> <RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" /> <FocusOnNavigate RouteData="routeData" Selector="h1" /> </Found> </Router> In WasmFileEditor project:
- Remove
Layoutfolder - Remove
App.razor - Reduce
Program.csby removing unnecessary elements - Remove "Layout" from
Imports.razor - Remove all pages other than the newly created
FileEditorcomponent
Remaining code in Program.cs in the WasmFileEditor:
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using WasmFileEditor; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); await builder.Build().RunAsync(); I get a run-time error when switching to Counter page where FileEditor component is added:
Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111] Unhandled exception in circuit bezeKQ89kvCeszvNMm1N8p1w6wo0QP_y3HZBEBM5_SU
System.NotSupportedException: Cannot create a component of type 'DemoServer.Components.Pages.Counter' because its render mode 'Microsoft.AspNetCore.Components.Web.InteractiveWebAssemblyRenderMode' is not supported by interactive server-side rendering.
Change in App.razor from this:
<body> <Routes @rendermode="InteractiveServer" /> <script src="_framework/blazor.web.js"></script> </body> To this:
<body> <Routes /> <script src="_framework/blazor.web.js"></script> </body> Page and component is shown, but the error is generated in console:
ManagedError: One or more errors occurred. (Root component type 'DemoServer.Components.Pages.Counter' could not be found in the assembly 'DemoServer'.)
Additionally, the breakpoint in Program.cs of the WasmFileEditor project where FileEditor component is located, is not hit.
What should I change to make it work?
Besides the error, I need to have HttpClient in the component, but Program.cs is not executed, so no DI available.







