We'll start working on our first Blazor project. In actuality, we'll make two Blazor projects. One uses client-side hosting, and the other uses server-side hosting. After that, we will run both projects to compare where and how they operate.

We will learn the distinctions between the two hosting types along with the benefits and drawbacks of each along the road.

Missing Blazor WebAssembly app template

The beta version of client-side blazor, commonly known as blazor WebAssembly, is still active. Therefore, you won't discover a Blazor WebAssembly App template when trying to create a Blazor project in Visual Studio 2022. Instead, you'll only find a Blazor Server App template.

Create project

To install the Blazor WebAssembly App template, use the command below.

dotnet new --install Microsoft.AspNetCore.Blazor.Templates::3.1.0-preview4.19579.2

This command can be run from the command line or the Package Manager Console in Visual Studio.

Blazor hosting models

The two hosting models available with Blazor are Blazor WebAssembly (client-side hosting model) and Blazor Server (Server-side hosting model)

client server and Webassembly server

Use the Blazor WebAssembly template to construct a Blazor application with a client hosting model and the Blazor Server App template to develop a Blazor application with a server hosting model, as indicated by the template names.

Blazor server Vs Blazor WebAssembly

The project structure and layout vary little between the two project kinds.

Webassembly Vs server

We shall go into detail about each of these files and folders later.

Multiple Startup projects can be configured in Visual Studio

In Visual Studio, we can set up many projects to be StartUp projects. The steps are as follows.

Set Startup Projects by right-clicking in the Solution Explorer on the solution name.

Setup project

Select the radio button for multiple startup initiatives. For each project you want to set up as a startup project, choose Start from the Action dropdown list.

multiple startup

Blazor WebAssembly Hosting Models

This hosting strategy uses WebAssembly to run the programme directly in the browser. The compiled application, its dependencies, and the .NET runtime are all downloaded to the client browser from the server as a result. Without a server connection, a Blazor WebAssembly programme can run fully on the client, or it can optionally be configured to communicate with the server through SignalR or web API requests.

client and server

Benefits of the Blazor WebAssembly Hosting Model

A Blazor WebAssembly software can run entirely on the client's computer. So, once the application has been downloaded, a server connection is not necessary. This implies that your server does not need to be online always.

The client receives work that was previously on the server. The resources and capacities of the client are being utilised.

The application can be hosted without a full-fledged ASP.NET Core web server. All we need is a server somewhere that can send the programme to the client browser. This means that we can host the application on our own server somewhere on the internet, in the cloud, on Azure as a static website, or even on a CDN Content Delivery Network.

The Disadvantages of Blazor WebAssembly Hosting

Hosting Model for Blazor Server

The programme is performed on the server in this hosting model. A SignalR connection is created between the client and the server. When a client event occurs, such as a button click, the information about the event is communicated to the server over the SignalR connection. The server processes the event, and a diff (difference) is calculated for the output HTML. The complete HTML is not provided to the client again; only the difference is sent via the SignalR connection. The browser then updates the user interface. The programme feels faster and more responsive to the user because only the diff is used to update the UI.

client server blazor

Advantages of Blazor Server Hosting Model,

The disadvantages of Blazor Server hosting are as follows,

Summary

I hope you understood model-hosting in Blazor.