As .NET 6 Nears GA, Blazor WebAssembly Can Now Use Native Dependencies

Less than a month before .NET 6 ships, Microsoft announced Blazor WebAssembly apps can now use native dependencies, allowing developers to tap into native C code, for example, upon jumping through a few hoops.

Blazor WebAssembly -- part of ASP.NET Core -- is the client-side component of Blazor, which allows for web projects coded primarily in C# instead of the normal dependence on JavaScript. Its new native dependency functionality came with the release of .NET 6 Release Candidate 2 this week en route to General Availability status around the start of the Nov. 9 .NET Conf 2021 event.

While ASP.NET Core and Blazor are now basically in the polishing-up stage of the development cycle along with other components of .NET 6 -- which is already being used in production as a "go live" release -- some new features were announced by Daniel Roth, principal program manager for ASP.NET.

"Blazor WebAssembly apps in .NET 6 can now use native dependencies that have been built to run on WebAssembly," Roth said in an Oct. 12 announcement. "You can statically link native dependencies into the .NET WebAssembly runtime using the .NET WebAssembly build tools, the same tools that you can use in .NET 6 to ahead-of-time (AOT) compile your Blazor app to WebAssembly or to relink the runtime to remove unused features."

As an example, he showed how to add a simple C function to a Blazor WebAssembly app, which is done with Emscripten compiler toolchain technology, upon which the .NET WebAssembly build tools are based. The site for Emscripten says it's "a complete compiler toolchain to WebAssembly, using LLVM, with a special focus on speed, size, and the web platform."

So those build tools have to be installed via an elevated command prompt (or Visual Studio Installer), along with the latest .NET 6 SDK and the latest preview of Visual Studio 2022 (which as of this writing would be VS 2022 RC). Then developers need to add NativeFileReference items to a project file, after which they can p/invoke into the native code from their .NET code.

Developers can also use libraries, such as NuGet packages, that contain native dependencies after again jumping through some hoops.

"The files for the native dependencies should be built for WebAssembly and packaged in the browser-wasm architecture-specific folder," Roth said. "WebAssembly-specific dependencies won’t get referenced automatically and need to be referenced manually as a NativeFileReference. Package authors can choose to add the native references by including a .props file in the package with the references."

To detail that, Roth demonstrated a project using SkiaSharp, a cross-platform 2D graphics library for .NET based on the native Skia graphics library. That whole process can be duplicated in a Blazor WebAssembly sample app in the SkiaSharp repo.

Hello SkiaSharp
[Click on image for larger view.] Hello SkiaSharp (source: Microsoft).

The other new feature highlighted by Roth is an update to minimal API support. He explained in detail changes affecting parameter binding, OpenAPI, source code analysis and breaking changes resulting from APIs being renamed.

About the Author

David Ramel is an editor and writer for Converge360.

comments powered by Disqus


Subscribe on YouTube