What's the purpose of "ASP.NET Core hosted" in Blazor WASM app?

Viewed 1233

When creating a new Blazor WebAssembly App in Visual Studio 2022 there's a "ASP.NET Core hosted" option (with a useless tool tip).

enter image description here

If you tick this instead of 1 WASM project in the solution, there are 3, Client (WASM), Shared and Server.

The Blazor Server app has no pages though it is the startup project. When you run it it calls the WASM Blazor Client app.

I can't find an explanation of what the purpose of doing this is? If I deploy a WASM app to an IIS server it runs fine without a Server app.

Note, I'm not asking the difference between Blazor Server (server side code running on the server that sends HTML to the client) and Blazor Client WASM (C# code compiled into WebAssembly that runs in the browser). I know this.

I'm asking why you'd want a Blazor Server app in a Solution with a Blazor WASM app if you intend to create a WASM app.

I can't find a direct answer to this. Either this article doesn't answer it - or I'm missing something: What's the difference between ASP.NET Core Hosted and Server-Side Blazor, really?

I can't find anything else.

So what is the purpose of the Blazor Server app, and why is it running first then calling the WASM app. Also how would you host that?

Thanks.

UPDATE

I'm aware that a WASM app is client side only, and you need, say, a Web API project to connect to in order to pull data from a web server.

Is this '.net Core Hosted' template doing something I couldn't do myself by creating a solution with a WASM project - then adding a Web API project (and various shared projects)?

1 Answers

Blazor server app almost works like a distributed desktop app. Every event on the browser is sent to the server, the server handles the event, calculates resulting changes in the DOM and sends the diffed DOM which is then applied to the page by the browser side js and the page is updated.

The use of traditional web API style pattern is completely optional here. In a Blazor server app you need not write a single web API for the app to work. Everything can be written as if it is a single desktop application.

A webassembly app is richer. The app actually runs on the browser, everything (event handling, UI refresh etc) is done on the client side only. These are so called 'static apps/websites' which, once loaded, require nothing else to execute. Imagine a single player, flash style games - they don't need connection to a database/application server.

However, there are hardly any rich apps which do not require even the basic database connectivity. So how does an app - say a gaming app that maintains a leaderboard - communicate with application servers and databases which maintain application data remotely? This is where the hosted model comes handy. In a typical web application scenario, you essentially need three components 1) Client side code - the app 2) Server side code - the web API - to maintain state 3) Shared object model - so that the first two can communicate seamlessly. That is what you see in a ".net core hosted" option. The template creates boilerplate for all three components. The classes that will be used by both client and server are put in the shared project. The server side and client side logic exists in their respective projects and both of these refer to the shared project without referring to each other.

Is it mandatory to have this to run a webassembly app? No. You can write your server side logic on any platform of your choice - it need not be asp.net - and call that API from the Blazor webassembly app . However, you will still end up creating mappers for client server communication classes. The .net core hosted option offers a very convenient way to do this.

Related