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

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube