WebAssembly & Auto

By Syncfusion®, Inc. · Last updated

Browser render modes in Blazor: Interactive WebAssembly project setup, @rendermode, the runtime and rendering flow, Interactive Auto and its server-to-WebAssembly switch, state across render modes and how to choose a render mode.

What you will learn

  • Set up Interactive WebAssembly with a Client project
  • Explain how the .NET runtime runs components in the browser
  • Configure Interactive Auto and explain the switch to WebAssembly
  • Keep state across render modes and choose the right mode

Interactive WebAssembly

  • In Server Rendering, your C# ran on the server. With Interactive WebAssembly, it runs in the browser.
  • WebAssembly is a fast kind of code that every modern browser can run. Blazor uses it to run .NET in the browser.
  • The browser downloads the .NET runtime and your app once. After that, clicks are handled right there, with no trip to the server.

Real-World Example:

  • Ravi wants a tea price calculator that customers can play with, even on a slow network.
  • With WebAssembly, the calculator runs on the customer's phone. Each click is instant.
  • The server only sends the files at the start.

The code boxes on this site already run this way. In the below example, OperatingSystem.IsBrowser() proves that the C# runs inside your browser.

Project Setup

  • --interactivity WebAssembly makes a Blazor Web App with two projects.
  • BlazorApp is the server project. It sends the pages and the files.
  • BlazorApp.Client holds the components that run in the browser. Interactive WebAssembly components must live here.

Click Next to see each command.

Two project folders side by side. TeaCalc, the server project, holds Program.cs, App.razor and static pages. TeaCalc.Client, the browser project, holds Program.cs and the interactive pages, like Counter.razor. An arrow shows the server sending the Client files to the browser.

TeaCalc (server) 📄 Program.cs 📄 Components/App.razor 📄 Pages/Home.razor (static) Runs on the server

TeaCalc.Client 📄 Program.cs 📄 Pages/Counter.razor 📄 _Imports.razor Runs in the browser

sends

A WebAssembly app has a server project and a Client project.

The server's Program.cs (shortened) turns on WebAssembly and tells Blazor about the Client project:

  • AddAdditionalAssemblies tells the server where the Client pages are, so their addresses work.

There is also a standalone WebAssembly app: dotnet new blazorwasm. It has no server project at all, so it can be hosted as plain files. This course uses the Blazor Web App.

Render Modes (@rendermode)

  • Put @rendermode InteractiveWebAssembly at the top of a page in the Client project.
  • Or set it on one component tag: <TeaCalculator @rendermode="InteractiveWebAssembly" />.
  • Or set it for the whole app on <Routes /> and <HeadOutlet /> in App.razor, like in Server Rendering.

Runtime and Rendering Flow

  • First visit: the server sends the prerendered HTML, so the page shows at once.
  • In the background, the browser downloads the .NET runtime (dotnet.wasm) and your app's files. The browser caches them, so later visits are fast.
  • The runtime starts in the browser, and the component becomes interactive.
  • From then on, a click runs your C# in the browser and updates the page directly. No server trip is needed.

Animation: the server sends HTML first. Then a stream of files, dotnet.wasm and the app DLLs, moves to the browser and is stored in a cache box. After that, a click dot moves only inside the browser, from the button to the C# code and back.

Server HTML first then the files

Browser

cache

.NET runtime

Add cup

your C#

dotnet.wasm, DLLs

The runtime downloads once. Then every click stays inside the browser.

In the below example, the C# reads facts about where it runs. The processor type is Wasm: WebAssembly in your browser.

Use Cases and Limitations

Good forLimits
Instant clicks, with no server tripA bigger first download (the .NET runtime)
Apps that keep working on a weak networkYour C# code can be downloaded and read. No secrets in the Client project
Less work for the server, so it serves more usersNo direct database access. Call a Web API on the server instead
Can work offline as an installable app (PWA)Slow phones run heavy code more slowly

In the below example, Ravi's price calculator runs only in the browser. Every change updates the price at once, with no server trip.

Never put passwords, keys or connection strings in the Client project. Anyone can download the files the browser runs.

Interactive Auto

  • Interactive Auto mixes the two interactive modes.
  • On the first visit, the component runs with Interactive Server, so it starts at once. Meanwhile, the browser downloads the WebAssembly files.
  • On later visits, when the files are ready, the component runs with Interactive WebAssembly.

Real-World Example:

  • A new customer opens Ravi's order page. It works straight away, using the server.
  • The next day, the same customer comes back. The page now runs in the browser, with no load on Ravi's server.

Project Setup

  • --interactivity Auto makes the same two projects as WebAssembly: a server project and a .Client project.
  • Auto components live in the Client project, because they must be able to run in the browser.

Render Mode Configuration

  • The server's Program.cs turns on both interactive modes, because Auto uses both.
  • Pages use @rendermode InteractiveAuto.

Server-to-WebAssembly Transition

  • Auto decides when a component starts. If the WebAssembly files are already in the browser, it uses WebAssembly. If not, it uses the server.
  • A component that already runs on the server does not move to the browser while you use it. The switch happens the next time the page or component starts, for example on the next visit.

Animation along two visits. Visit 1: the page uses the server, shown by a live line to the server, while a progress bar of WebAssembly files fills up in the browser. Visit 2: the line to the server is gone and the page runs in the browser.

Visit 1

Order page runs on: Server

downloading WebAssembly files...

Server

later

Visit 2

Order page runs on: WebAssembly no live server line needed

Auto: the first visit uses the server while files download. Later visits run in the browser.

An Auto component runs on the server and in the browser at different times. Write it so it works in both places: get data through a Web API, not straight from the database.

In the below example, the page shows which mode it got. In an Auto app it says Server on the first visit and WebAssembly later. The code box always runs in the browser, so here it says WebAssembly.

Use Cases and Limitations

Good forLimits
A fast first visit and a light server laterTwo projects and two modes to understand
Public sites with many returning usersCode must work in both places: no direct database calls
Saving server memory over timeState does not move from the server to the browser

State Management

State Across Render Modes

  • Server memory and browser memory are separate. Values in a component on the server do not travel to the browser.
  • When a component starts in a new mode, or the page reloads, its fields start again from the beginning.
  • To keep data, store it somewhere that survives:

    • During prerendering: [PersistentState], which you met in Server Rendering.
    • For the user's browser: browser storage, like localStorage.
    • For everyone: a database on the server, reached through a Web API.

Two boxes, Server memory and Browser memory, with a crossed-out arrow between them: values do not move. Below them, three places that keep data: PersistentState (prerender), localStorage (browser) and Database (server).

Server memory cups = 3

Browser memory cups = 0

✗ Keep data here instead:

[PersistentState]

localStorage (browser)

Database (server)

Memory does not move between server and browser. Store data where it survives.

In the below example, CupSaver.razor keeps the cups in the browser's localStorage, using IJSRuntime to call JavaScript. Click Add cup a few times, then Hide and Show: the count comes back.

  • The value is read in OnAfterRenderAsync, because JavaScript is only ready after the first render.
  • localStorage belongs to one browser. Another phone or a different browser starts at 0.

Choosing a Render Mode

  • Start with the simplest mode that does the job, and only add more when a page needs it.
  • Remember: each page or component can pick its own mode.

A decision path. Does the page need clicks or live updates? No: Static SSR. Yes: Must the code or data stay on the server, or is the first load important? Yes: Interactive Server. Should it work on weak networks and save server work? Yes: Interactive WebAssembly. Want a fast first visit and the browser later? Interactive Auto.

Needs clicks or live updates?

no

Static SSR

yes

Server data, secrets, small devices?

Interactive Server

Weak network, light server?

Interactive WebAssembly

Fast first visit, browser later?

Interactive Auto

Ask the questions from the top. Stop at the first answer that fits.

Static SSRInteractive ServerInteractive WebAssemblyInteractive Auto
Clicks workNoYesYesYes
First viewFastestFastSlower (download)Fast
Needs a live connectionNoYesNoOnly at first
Server memory per userNoneYesNoneAt first
Works offlineNoNoPossibleAfter the download
Can reach the database directlyYesYesNo (use an API)No (use an API)
  • For Ravi: the Menu is Static SSR, the Order page is Interactive Server, and the price calculator is Interactive WebAssembly.

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
An Interactive WebAssembly page in the server projectAny folder worksPut it in the .Client projectThe page cannot become interactive
A connection string in the Client projectClient code is privateKeep secrets on the server onlyAnyone can read the secret
Calling the database from a WebAssembly componentIt works like the serverCall a Web API on the serverThe component cannot reach the database
No AddAdditionalAssemblies in Program.csThe server finds Client pages by itselfAdd the Client assemblyClient pages show Not Found
Expecting Auto to move a running page to the browserIt switches in the middleIt decides when the component startsStill on the server until the next visit
Expecting values to move from server to browserMemory is sharedSave data in storage or a databaseThe values start again at 0

Check Yourself

Exercises

What to Remember

  • Interactive WebAssembly runs your C# in the browser. Clicks need no server trip.
  • --interactivity WebAssembly or Auto makes a server project and a .Client project. Browser components live in .Client.
  • Program.cs needs AddInteractiveWebAssemblyComponents, AddInteractiveWebAssemblyRenderMode and AddAdditionalAssemblies.
  • The .NET runtime downloads once and is cached. No secrets and no direct database calls in Client code.
  • Interactive Auto uses the server first, then WebAssembly once the files are in the browser. It decides when a component starts.
  • Memory does not move between server and browser. Keep data with [PersistentState], browser storage or a database.
  • Choose the simplest mode each page needs: Static SSR, then Server, WebAssembly or Auto.

Next: JavaScript Interop