Blazor at the tershouse meetup

on

I gave a talk on Blazor at the tershouse meetup on April 14th 2020. These meetups are normally held in Sarajevo (Bosnia and Herzegovina) at the tershouse coworking space. However, due to the Covid 19 situation, the talk was held online this time. Regardless, I hope to come to Sarajevo soon to meet Nermin and Amar live and have some ćevapi.

Blazor – is C# the one ring to rule them all?

Microsoft has built Blazor, and I love the idea behind it. The goal of writing C# anywhere is an ambitious one, but if they make it, I will definitely love them for it. I have written about Blazor previously, but this was my attempt in giving a general overview of where it currently is, what the cool things I like about it are and where Microsoft is supposedly planning to go with it next.

Slides from the presentation available here.

Code for the demos from the presentation available here.

More information on some answers

The Q&A session was awesome, in my opinion, and there were quite a lot of cool and interesting questions. I answered most of them, but I would like to provide additional information and links here.

Performance of Blazor vs existing JS frameworks

There was a question about comparing the performance of Blazor and Javascript. So – Blazor Wasm needs to download the whole .NET Core runtime compiled into Wasm to the browser. Currently this is quite large, so it makes the initial load quite slow. However, once the code is actually loaded into the browser – it is native binary code run in the browser and it should be pretty fast. To conclude, I’d like to share a few links comparing the performance of Wasm and Javascript:

You can call JS from Blazor using JS Interop. Can you do vice versa, call Blazor code from JS?

In short, yes. You need to use DotNet.invokeMethod or DotNet.invokeMethodAsync combined with the [JSInvokable] attribute:

Razor code:

<button type="button" class="btn btn-primary"
        onclick="exampleJsFunctions.returnArrayAsyncJs()">
    Trigger .NET static method ReturnArrayAsync
</button>

@code {
    [JSInvokable]
    public static Task<int[]> ReturnArrayAsync()
    {
        return Task.FromResult(new int[] { 1, 2, 3 });
    }
}

Javascript:

window.exampleJsFunctions = {
  showPrompt: function (text) {
    return prompt(text, 'Type your name here');
  },
  displayWelcome: function (welcomeMessage) {
    document.getElementById('welcome').innerText = welcomeMessage;
  },
  returnArrayAsyncJs: function () {
    DotNet.invokeMethodAsync('BlazorSample', 'ReturnArrayAsync')
      .then(data => {
        data.push(4);
          console.log(data);
      });
  },
  sayHello: function (dotnetHelper) {
    return dotnetHelper.invokeMethodAsync('SayHello')
      .then(r => console.log(r));
  }
};

This example was taken from Microsoft’s official documentation. Please go through it for additional options and explanations.

Is it possible to decompile Wasm back to code?

It is possible, though it is difficult. Check this discussion on the Webassembly GitHub.

However, it is possible to do it and there are tools that do it.

Not only that, but according to a discussion on aspnetcore’s GitHub, they cannot (and will not) do anything directly to prevent it. Though, as the code for Wasm is going to be just a DLL file, there will be an option to use existing code obfuscators to fight your enemies.

Lazy loading in Blazor

Lazy loading is currently not available (out of the box) in Blazor Wasm. The community is asking for it and, according to a discussion on aspnetcore’s GitHub, it has been added to the backlog.

On the other hand, people have developed their own solutions, so it does seem like something that is doable and that will be available in the near future.

State Management in Blazor

As I said in the talk, this should really be a topic of its own, both for a separate blog post as well as for a separate talk. This is something I might do, eventually.

But for now I’ll just share a few resources here that I really find useful:

Blazor application to an .exe file

If I understand the question correctly, the question was about creating standalone desktop applications using Blazor, and have them execute as an .exe file.

An example of this are the Electron apps that have been mentioned in the talk. A good basic tutorial is given by Nick Chapsas.

Conclusion

For me this experience was amazing. I’d love to thank tershouse and especially both Nermin and Amar for making this possible for me. I’d also like to thank everyone who participated, especially the people who asked all the cool questions. Thanks for making me work a bit harder and making me do additional research.

I am looking forward to see how Blazor will develop (as well as how .NET Core itself will develop). In my opinion, we are looking at exciting times ahead.

Leave a Reply

Your email address will not be published. Required fields are marked *

You are currently offline