News

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

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube