Developer's Toolkit

Will Blazor Save .NET Developers from 'the Insanity of JavaScript'?

Question: Is Microsoft working on .NET targeting WebAssembly so that we can get delivered from the insanity of JavaScript? Answer: Yes.

No programming language sparks debate like JavaScript, with many developers coming down on the "hate it" side of things when discussing the notoriously complex ecosystem.

Though I've come to appreciate it more since I started fooling around with React Native, the loosey-goosey nature of JavaScript often still confounds me.

Thus I was interested in a little tidbit of information resulting from yesterday's GitHub "Ask Me Anything" (AMA) event with Scott Hanselman from Microsoft's Web Platform Team.

It concerns Blazor, an open source Visual Studio extension (hosted on GitHub) described as "An experimental Web UI framework using C#/Razor and HTML, running client-side via WebAssembly."

Here's the Q&A that prompted Hanselman to mention Blazor:

Q: Is MS working on .Net targeting WebAssembly so that we can get delivered from the insanity of JavaScript?
It would be really nice to have a "SilverLight like" Web development experience where we have a Xaml UI framework that replaces the DOM and code can be written in C#/F# instead of JavaScript. Is Microsoft working to make that happen with .Net and WebAssembly (or any other technology)? Thanks!

Hanselman: Yes https://github.com/SteveSanderson/Blazor.

Going to that link presents Blazor's README.md file:

"The arrival of WebAssembly creates the possibility of building client-side Web applications using languages and runtimes that are more typically used for native app development. Blazor runs .NET code in the browser via a small, portable .NET runtime called DotNetAnywhere (DNA) compiled to WebAssembly.

"The programming model will be familiar to anyone who's worked with Razor (the C#/HTML page format used by ASP.NET MVC and ASP.NET Pages)."

WebAssembly is an open, new-age format standard -- with an initial version that has reportedly reached cross-browser consensus -- described as "a new portable, size- and load-time-efficient format suitable for compilation to the Web."

The Mozilla Developer Network notes that it's experimental technology, and describes it like this:

"WebAssembly is a new type of code that can be run in modern Web browsers -- it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++ with a compilation target so that they can run on the Web. It is also designed to run alongside JavaScript, allowing both to work together."

The UK-based Steve Sanderson, part of Microsoft's ASP.NET team, has put WebAssembly to work in his own personal Blazor project, a Visual Studio extension that can be downloaded from the GitHub site. It's not production-ready and isn't available in the Visual Studio Marketplace.

The project leans heavily on Dot Net Anywhere, an interpreted .NET Common Intermediate Language (CIL) runtime that lets .NET software run on resource-constrained devices that can't handle a full .NET runtime, likeĀ Mono.

The Blazor GitHub project has received contributions from four developers, earning 1,085 stars.

It provides a project template to create a standalone Blazor app, not hosted on an ASP.NET server. "The idea is that ultimately you could deploy your client-side Blazor app as a set of purely static files that could be served from any web technology (Rails, PHP, a static file host, etc.)," Sanderson says on the site. "Therefore, server-side prerendering is not supported in this mode."

Using it requires installations of NET Core 2.0 preview 3 or later and Visual Studio 2017.3 or later.

The project has its own Q&A, including:

Could something like this really be practical? How big would the apps be to download?
Yes, it could be practical. A "hello world" app that runs Razor in the browser with this template is around 300KB, which is smaller than typical apps from some other single-page-app (SPA) frameworks. That 300KB includes everything: the small .NET runtime, core libraries, application code, and wrapper libraries needed to bootstrap and interop with the WebAssembly code.

That's before any real optimization attempt. If there was a proper attempt at code stripping to remove all non-called corlib code, it could get smaller.

What about browsers that don't support WebAssembly?
It still works fine there too. WebAssembly is designed to fall back on a JavaScript polyfill easily. The template in this repo automatically detects if the browser doesn't support WebAssembly, and loads an asm.js version instead. Performance is still surprisingly good.

Is this actually .NET in the browser?
It's not the regular .NET Framework or .NET Core runtime. It's a third-party .NET runtime called DotNetAnywhere, which has been updated and extended in various ways to support being compiled to WebAssembly, to load and run .NET Core assemblies, and with some additional functionality such as basic reflection and so on.

Can I build a real production app with this?
No. It's incredibly incomplete. For example, many of the .NET APIs you would expect to be able to use are not implemented.

Why isn't there an issue tracker? How do I file issues?
You don't file issues. There's no support for this. It's experimental.

If you would like to contribute, you can submit a pull request though.

Why does this exist?
To see how well such a framework might work, and how much anyone would care. Note that it's a personal project rather than an official Microsoft project.

More information on Blazor can be found in a July podcast on .NET Rocks! and a Reddit discussion. For more on WebAssembly, check out the brand-new blog post just published today, titled "WebAssembly: A New Hope."

As Blazor is an experimental project based on the experimental WebAssembly, it has a long way to go before you're likely to use it for a real-world project -- if it ever evolves that far. Nevertheless, for Web developers looking to escape JavaScript from hell, it might be worth keeping an eye on.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus

Featured

Subscribe on YouTube