Why Was Blazor Invented?
In 2015, vendors in a W3C Community Group created WebAssembly. It is a byte code that can run in modern web browsers and provides new features and significant gains in performance. It is designed as a compilation target for source languages like C, C++.
Benefits of Using WebAssembly
- Encoded in an efficient binary format.
- Runs at native speed by using standard hardware capabilities.
- The textual format makes it easy for debugging and testing.
- It is safe due to the execution environment that is sandboxed and describes a memory safe.
In .NET code, WebAssembly code generation uses Mono through a command-line tool called mono-wasm. Blazor sends .NET codes to Mono and enables WebAssembly code to allow .NET code to run in a browser.
Millions of .NET developers are excited about Blazor because it is based on C#, Razor, and other Microsoft technology. Also, it is supported by Visual Studio and Visual Studio Code.
Blazor handles user interaction as a UI event and not in the context of an HTTP request. It also provides ongoing UI updates.
Blazor web application consists of one or more root components that are rendered on the HTML page. Each component manages its state and specifies its rendering logic. To update the components, each triggers the event based on the specific user interaction.
During component rendering, Blazor keeps track of the changes in the output. It does not render directly to Document Object Model (DOM). The changes are rendered into an in-memory representation of DOM called RenderTree, allowing Blazor to calculate the UI diff between the new and previous state. The UI diff is then applied efficiently to the DOM.
Blazor is primarily a client-side web framework built to run in a browser within a single process. However, that approach could not accommodate the ability to run a web app within multiple functions in a browser or a separate process when writing a desktop application with Electron. In Blazor Server version 0.5.0, apps have two hosting models.
Blazor WebAssembly Apps
Blazor WebAssembly apps runs purely on the client-side and does not require .NET in the server at all. However, to maximize the benefits of using Blazor, you can host Blazor WebAssembly apps with ASP.NET Core hosting. It allows the user a full-stack experience by building your app with one consistent language, frameworks, and tools with .NET web development. And, by using .NET, you can easily share code with both clients and servers.
Benefits of Using Blazor WebAssembly
- Dependency is not required from the .NET server-side.
- Client resources and capabilities are fully utilized.
- Work is moved from the server to the client.
- It’s possible to have serverless deployment scenarios.
Blazor WebAssembly supports delivering an application as PWA as it is based on WebAssembly.
Blazor Server Apps
The app runs on the server in this hosting model instead of from a client’s browser. The rendering process is decoupled. This enables UI events to be sent from the browser. Then the UI diff is sent from the server over a real-time connection. Thus, RenderTree is processed on the server, as explained in the Architecture section.
Benefits of Using Blazor
- Can use existing .NET libraries.
- Shared logic across servers and clients.
- Improved .NET performance, reliability, and security.
- Increased productivity with Cross-platform Visual Studio.
Overall, Blazor provides future Web UI frameworks for .NET developers, and other developers need to learn only one set of languages and frameworks.
Author: Gilang Jalu Selo – Analyst Programmer