DEV Community

Ali Kolahdoozan
Ali Kolahdoozan

Posted on

Is .NET 9 beneficial for Blazor?

Image description

Blazor, an integral part of Microsoft's ASP.NET Core framework, seamlessly integrates with back-end technologies, making it a powerful tool for modern web application development.

Blazor enables developers to create interactive web applications using C# and .NET instead of JavaScript. It offers a full-stack solution for building enterprise applications that feature sophisticated, interactive front-ends paired with highly scalable back-ends. The release of .NET 9 for Blazor brings to mind the impact of .NET 6, especially considering that the prior version, .NET 5, was a significant turning point.

In 2020, .NET 5 unified the benefits of both the .NET Framework and .NET Core into a single platform simply called .NET. This major upgrade made .NET 5 the first version of a completely unified .NET environment. Notably, .NET Framework 4.8 was the final iteration of the traditional framework, and .NET Core 3.1 was the last of .NET Core.

Following this unification, .NET 6 was released in 2021. While the upgrade from .NET 5 to .NET 6 wasn't as monumental, it solidified the unification of the platform. Similarly, the release of .NET 9 in 2024 further cements the game-changing enhancements made to Blazor—such as improvements introduced with .NET 8—along with new features and refinements.

Therefore, .NET 9 represents another substantial upgrade, especially regarding Blazor enhancements. Microsoft's continued investment in Blazor is encouraging, solidifying C# as a viable language for full-stack enterprise web and hybrid applications. Developers can now write C# code in a single codebase that runs in browsers as well as on native platforms like Android, iOS, macOS, and Windows. .NET 9 reaffirms that Blazor is a framework with lasting significance.

With .NET 9, Blazor WebAssembly applications launch 25% faster based on Google Lighthouse scores. Blazor Server applications are more responsive thanks to WebSocket message compression. Additionally, static web assets are optimized through pre-compression and fingerprinting, enabling efficient downloading and caching.

Web developers are often frustrated when updates to CSS files don't reflect immediately for users due to caching issues. Fingerprinting addresses this problem by ensuring that changes are instantly propagated to clients. This improvement is a welcome relief for those who've experienced this ordeal.

Let's delve into .NET Aspire. It's a suite of robust tools, templates, and packages designed for building observable, production-ready applications. Delivered through a collection of NuGet packages, it addresses specific cloud-native requirements. Cloud-native applications typically consist of small, interconnected microservices rather than a monolithic codebase.

One advantage of ASP.NET Core is the ease of integrating .NET components into larger systems. For instance, adding the .NET Aspire orchestrator to Blazor applications via Visual Studio is straightforward. This orchestrator manages and coordinates workflows, processes, and service interactions within an application.

Serving as a centralized controller, it ensures smooth communication between various components like microservices, APIs, and external systems. Key features include workflow management, service coordination, fault handling with resilience mechanisms like retries and exception handling, and monitoring and logging for performance insights.

Scalability is also supported, allowing dynamic scaling to handle increased loads by distributing tasks across multiple services. Integration support connects internal application services with external APIs or third-party systems seamlessly.

For example, consider a solution with a Blazor component and a Web API component. By adding .NET Aspire orchestrator support via Visual Studio, additional projects and middleware configurations are automatically included. Developers don't need to focus on these details, as the solution is configured to leverage the orchestrator's benefits. This setup keeps components loosely coupled while orchestrating their interactions with minimal additional code.

As applications scale with more components, this becomes increasingly beneficial. Once .NET Aspire is properly configured, features like service discovery automate the identification of service instances, their locations, and availability without manual configuration.

When running your app in Visual Studio, a generated dashboard displays all the components of your solution. You can navigate to component endpoints, test various aspects, view logs, and inspect workflow traces—all from a central location. Monitoring the health of components becomes straightforward with built-in health checks that provide real-time feedback for diagnosing issues.

.NET Aspire also handles automatic retry management for components. This resilience mechanism manages transient communication failures between components or external services. For instance, if a network issue occurs while a Blazor component tries to communicate with a Web API component, the workflow won't immediately fail. The Blazor component will periodically retry communication, with configurable intervals and retry limits before throwing an exception. This functionality reduces the need for extensive custom code.

Returning to the .NET Aspire dashboard, it's noteworthy that Microsoft built it using Blazor and Fluent UI.

Now, let's discuss some Blazor-specific improvements in .NET 9. One enhancement is the ability to detect a component's render mode at runtime using the new Render Info API. This API helps determine whether a component is rendering interactively or via static server-side rendering (SSR).

At runtime, you can decide whether to render a button or display status information indicating that the page isn't fully loaded yet. This allows developers to leverage the increased startup speed of SSR, so content appears almost immediately when a user accesses the page. Interactive elements are displayed once the component is ready, significantly enhancing user experience by providing smoother interactions.

Significant enhancements have also been made to reconnection logic, particularly concerning Blazor interactive server components and the SignalR connection. In .NET 9, Blazor's reconnection logic has been substantially improved to boost reliability and user experience. Enhanced WebSocket handling now manages disruptions more effectively, ensuring smoother transitions during temporary disconnections.

Additionally, you can blend interactivity and static SSR within a single Blazor app, enhancing flexibility and performance. Static server-side rendering can serve pages that don't require interactivity, improving performance and SEO. This optimization offers extremely fast initial load times for static pages, enhancing user experience.

Blazor authentication state serialization has been simplified. With .NET 9, serializing and deserializing the authentication state is easier for applications utilizing both server-side and WebAssembly components. Developers can use built-in APIs to streamline authentication state management without manually writing extensive code.

Component constructor injection in Blazor with .NET is now supported. This allows dependencies to be provided to components via their constructors, utilizing the dependency injection framework built into ASP.NET Core. It's especially useful for injecting services, configurations, or other objects into components directly through constructors rather than public properties.

The Blazor Hybrid Plus project template is among the most exciting improvements. The .NET 9 Blazor Hybrid Plus web project template enables developers to create applications that share a single UI across both a .NET MAUI Blazor hybrid app and a Blazor web app. This template streamlines development for apps targeting native platforms like Windows, iOS, and Android via MAUI, as well as the web, by leveraging a shared Razor Class Library (RCL). This makes it easier to target multiple platforms from a single codebase.

Clearly, with .NET 9, Microsoft has made Blazor even more impressive.

Top comments (0)