Wednesday, November 27, 2019

Blazing a Trail - C# in the Browser


I realized, after re-reading, that I've been referring to Blazor when I should really be talking about Blazor WebAssembly. The other form of Blazor is Blazor Server. That's not what I'm writing about here.

I didn't come to .Net very willingly, I resisted the Microsoft technology stack quite strongly back in the day, when they were very monopolistic and I didn't like the lock-in that I felt when you entered their walled garden (I still don't like the Apple walled garden either, in case you're wondering). But something started to happen at Microsoft just before and since Satya took the reins. They started to contribute to open source and build tools for other platforms. They were contributing to projects and being (apparently) collaborative rather than combative where their tools overlapped with existing ones.

And one of the biggest places I saw this was in web tooling. Visual Studio started to consume some of the biggest and best open source web tools (Nodejs, Grunt, Bower, etc.). These were tools that web developers were already using in their own projects and which, I think, Microsoft realized were good quality and would actually ease the transition for many developers into a more Microsoft world.

And that adoption and acceptance of non-Microsoft tools hasn't slowed down. In some cases it has meant that the tools become part of the Microsoft (Github) and in other cases it has meant relying on tools produced by other giants in the technology space (SharePoint relies heavily on ReactJS from Facebook). This shows, to me at least, how Microsoft has changed over the past 10 years.

But this isn't a post about Microsoft, it's about web technologies and Blazor in particular.

Blazor? - It's Building a Web App in C# (literally)


But first, let's talk briefly about WebAssembly (WASM), a technology that essentially bypasses JavaScript to provide a new and highly flexible way to run code in a browser. Unlike competing languages (Dart) for the web, WASM instead provides the ability to run compiled code in a browser. WASM lives and works like the virtual machine in the JVM or the runtime in the CLR. If you compile your code against WASM then it will run in any compatible browser. It's the browser/web version of a virtualized CPU it is an assembly language for the web :o I wonder if that's how the figured out the name...?

So I've still not go to Blazor. Ha, that's because I think the introduction is needed so you grok what WASM is so that you can actually believe what follows and how it can be true. To me, it still feels incredible that I can do the things of which I am going to speak.

So I said you can compile whatever you like to WASM, so someone at Microsoft decided to try and compile the .Net runtime (the Mono runtime actually). And then what do you get? Well, you get a compiled output file (mono.wasm in this case) which can be downloaded into the browser and which will then run just like the .Net runtime does on any other platform (almost). Which means that I now have code, running in a browser, that can execute DLLs; the very same DLLs that I can run locally on my Windows machine.

I'm sorry to labor the point but these are C# programs compiled just like any other. They don't include any JavaScript, there's no transpilation, they are POCOs built using MSBuild and they can be run like any .Net code you could build. The only thing that's different is that with the browser version, they are executed by the Mono WASM runtime and not the .Net Framwork.

To me, that's huge.

So, Blazor?


Right, Blazor. Well now you appreciate the "What" about Web Assembly, then Blazor is your "How", the means to get it all working and get some value.

Blazor is the blending of Browser + (a hidden "L" that crept in there) + Razor

It's a framework for building web applications using C# and only C#. You can write components (a little bit like ReactJS components) in C# and consume them in C# and use Razor to integrated C# with HTML. If you're a C# ASP.Net or Web API developer already, you can just use the tools you already know.

I'm not going to go into detail on how the Blazor framework works, the documentation on MS docs is good and getting better all the time. The developers are active on Github, where the project (as part of ASP.Net Core) is being built, and seem to be very active engaging with the community. If you want to give it a go, check out the Get Started page in the docs and note that you will need to install .Net Core 3.1 to get Blazor WebAssembly.

I'm not sure what the long term viability of Blazor is, but I'm hopeful that WebAssembly will allow developers to explore real alternatives to running code in the browser. I'm excited to see what they come up with.