Blazor Improvements in .NET 6

7/3/2021
4 minute read

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.

EF Core 6 - New Features

EF Core 6 is right in front of our door and it looks very promising. In this blog post I will show what new features are to come.

Prerendering Blazor Apps - How does it work / tips and tricks

Blazor comes with the option to prerender your webpage on the server. This works for the client-side version as well as the server side version of Blazor.

Let's have a look how does it work and what might be some pitfalls and how can we come around those.

ASP.NET Core - Why async await is useful

Did you ever wonder why you "should" use async and await in your ASP.NET Core applications? Most probably, you heard something about performance. And there is some truth to it, but not in the way you might think.

So let's discuss this with smaller examples.

An error has occurred. This application may no longer respond until reloaded. Reload x