Blazor Improvements in .NET 6

03/07/2021

What to come?

ASP.NET Core Logo

If you are interested in what will come with ASP.NET Core in .NET 6 you can follow the official roadmap on github

Here are some things already implemented:

  • Hot Reload for ASP.NET Core / Support Code Hot Reload (EnC) for Razor
  • WebAssembly AoT Compilation
  • Required parameters to blazor components
  • Allow generic type constraints

Let's have a look at some of those features and what they mean:

WebAssembly AoT Compilation

Microsoft's Daniel Roth last year said: The .NET runtime used by Blazor WebAssembly is doing IL interpretation -- there's no JIT -- so this does affect the performance of CPU intensive workloads. For .NET 5 we are looking at adding support for ahead of time (AoT) compilation that we enable precompiling hot paths to WebAssembly as a way to improve runtime performance.

Well as this did not come with .NET5 it is now included in Preview 4 of .NET6.

What does that mean?

For starters you will gain a performance boost when it comes to CPU intensive tasks. Right now the C# code you write will interpreted at runtime to webassembly code. Only when the code is really run .NET will translated the IL code to WebAssembly. With AOT the code will directly compiled to WebAssembly. Nice, isn't it?

Required parameters to blazor components

One of my favourites as it can make your intensions clearer when providing an API. Just image the following Blazor component from an user point of view:

<MyComponent Title="My Header" Subtitle="My Subtitle"></MyComponent>

But the implementation offers you the following parameters:

...
@code {
    [Parameter]
    public string Title { get; set; }

    [Parameter]
    public string Subtitle{ get; set; }

    [Parameter]
    public string AdditionalInformation{ get; set; }
}

Me as a consumer of that I have no idea what is required by the component or not. Do I have to provide all the parameters?

And here the new feature kicks in, you can annotate so that you are able to tell which parametes are required

@code {
    [Parameter(Required = true)]
    public string Title { get; set; }
}

Reimplement the Razor compiler using source generators & support incrementality

That is a very nice one. .NET6 tries to improve the inner-loop performance. Right now every slight change of a razor component will lead to a complete build of this asset. With the SourceCodeGenerators introduced with .NET5, Microsoft aims to remove some compiler steps so that the overall build chain will be faster.

Other features which might come

Drag and Drop in .NET6

Right now drag and drop has to be done solely with Javascript-Interop. But that makes it hard if you want to right a component. Especially when using server side blazor where you are only allowed to link <script> tags or files in the _Host.cshtml. Bye bye writing nice encapsulated components. This might change with .NET6.

Another use case (for example for this blog) is drag and drop of files on textarea components to allow upload of files. Right now the property is always null. This also might come with .NET 6.

Hot Reload for Blazor

With Hot Reload you can now modify your apps managed source code while the application is running, without the need to manually pause or hit a breakpoint. Simply make a supported change while your app is running and in Visual Studio use the “apply code changes” button to apply your edits.

Disclaimer

All those features are in preview and the final shape can be changed. The given example are based on the Preview 5 of .NET 6.

0
Buy Me a Coffee at ko-fi.com
An error has occurred. This application may no longer respond until reloaded. Reload x