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 at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube