asp.net-coreblazorblazor-webassemblymaui-blazor

.NET 9 Blazor Hybrid with web app. Accessing WebAssembly DI


In the Visual Studio template for .NET MAUI Blazor Hybrid and web app template with "InteractiveWebAssembly", I get the following projects:

There is a service interface IFormFactor defined in the shared project with an implementation in each of the other three projects.

When I start my solution with the web project as the startup project, I can see that the first load of the page used the web implementation of IFormFactor, and if I navigate away and come back, then I see it using the Web.Client implementation of IFormFactor.

I understand that this is due to server pre-rendering.

In my case I only want to use services from the client. How can I avoid that pre-rendering so the first render uses services injected from the client DI, instead of the server DI?


Solution

  • Found the answer.

    In my Web project's App.razor file I had:

    <HeadOutlet @rendermode="InteractiveWebAssembly" />
    <Routes @rendermode="InteractiveWebAssembly" />
    

    This resulted in pre-rendering.

    Changing these to:

    <HeadOutlet @rendermode="new InteractiveWebAssembly(prerender: false)" />
    <Routes @rendermode="new InteractiveWebAssembly(prerender: false)" />
    

    Prevented pre-rendering on the server and my services are injected from client DI on first load of page.