This page looks best with JavaScript enabled

Hosting Models for Blazor

 ·   ·  ☕ 4 min read

Blazor has three hosting models - as I see it :)

  • Server
  • Client
  • Client++

We will look at them in brief below.

Server

Server hosted implies the entire application being hosted on server. A razor-thin (no pun) app is delivered to client and there on client relies on server for everything.

I mean everything -

  • All clicks, user input, gestures etc. will be transferred to server through a SignalR connection. Server figures out what to do
  • Server does DOM diffing, keeps track of updates that are done client-side and what should be sent to client. Server delivers those changes to client over the ever-persistent SignalR
  • Server sees any connection drops and maintains state if client reconnects
  • Client reacts, and keeps reacting with help from server
  • Can work on older browsers (though, to be frank, if you are using old browsers - you should be upgrading old browsers and the apps rather than reading this article)

Though this may sound primitive, but works wonderfully well - provided you have good connectivity to server and a fast-enough server.

  • We have no business logic going to the client - server takes care of everything
  • Infrastructure is under tight control and you can do whatever you please (well, it’s our server)

At the same time -

  • There will be delays. It is better to do processing locally than on the server. It will be interesting to see real-world performance, but I do not expect this to be fast
  • Everything is dependent on server - so strictly online feature set
  • Client-side connections and all that work will put significant stress on servers (I was not a fan of such client connections in MeteorJS, I really doubt server-side Blazor will change my mind)

I think server-hosted models will be a boon to a whole lot of stupid enterprise applications that have been begging for an upgrade since Y2000. I see server-hosted Blazor as the new client-server - only that we now follow universal standards :).

Server-side hosting is also easier to control and implement - Microsoft plans to deliver these as part of the .NET Core v3 full release later this year. The only positive I see here is the web-socket like SignalR. We do not need to write a lot of boiler-plate to control those server-client interactions but let the underlying framework handle them.

Client

Client-side hosting falls to the other extreme. The app delivers itself to the client and runs as a full-fledged .NET application using Mono within the browser.

Once the app DLLs (yes, DLLs) and WASM running infrastructure is downloaded to client, the client does not need the server anymore.

  • All processing happens on the client side (except online connections and updates to server DB, of course)
  • UI generation, reacting to changes and events happen on the client side

I could also deliver this blog entirely to your desktop - take that, Blogspot.

On the flip side -

  • There is much more dependence on browser to stick to WASM standards and get everything done
  • Initial download size is larger. It is not so large to cause a commotion - we see MBs getting downloaded from style libraries even today
  • WASM is comparatively young technology, and may not be the best choice today for applications with stability as a requirement

The ++ Sub-division and Selecting a Hosting Model

When you click on New Project in Visual Studio, and select Blazor template, you will see the exact two options outlined above.

In addition to the options, you will also see a checkbox called ASP.NET Core Hosted for Blazor WebAssembly App. This is the result of a simplified model in Preview 8, and which I have come to appreciate.

By selecting the box, you are instructing the the scaffolding to create -

  1. Client-side code
  2. Server-side code that can enable your app to have server-side logic including those done by services/controllers in a MVC model
  3. Shared code, which can be shared by client and server

Any web app is not an island by itself. Using a core hosted model with WASM, we can create applications that do most of the work on the client-side, but can also fall back on server for sensitive/proprietary business logic, maintaining centralized data store, and other such use cases.

I am personally excited about the server-hosted WASM. That is the future, folks - does not matter whether you mark or unmark my words.

Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things