Blazor WebAssembly can now use native dependencies – Visual Studio Magazine
As .NET 6 approaches general availability, Blazor WebAssembly can now use native dependencies
Less than a month before the release of .NET 6, Microsoft announced that Blazor WebAssembly apps can now use native dependencies, allowing developers to tap into native C code, for example, by jumping through a few hoops.
While ASP.NET Core and Blazor are now essentially in the refinement phase of the development cycle with other components of .NET 6 – which is already in production as a “go live” version – some news features were announced by Daniel Roth, Senior Program Director for ASP.NET.
“Blazor WebAssembly applications in .NET 6 can now use native dependencies designed to run on WebAssembly,” Roth said in an Oct. 12 statement. announcement. “You can statically bind native dependencies to the .NET WebAssembly runtime using the .NET WebAssembly build tools, the same tools that you can use in .NET 6 to back-compile (AOT) your Blazor application to WebAssembly or to re-link the runtime to remove unused functionality. ”
As an example, he demonstrated how to add a simple C function to a Blazor WebAssembly application, which is done with the Emscripten compiler toolchain technology, which the .NET WebAssembly build tools are based on. The site for Emscripten says it is “a complete compilation toolchain for WebAssembly, using LLVM, with special emphasis on speed, size and web platform.”
These build tools must therefore be installed via an elevated command prompt (or Visual Studio Installer), with the latest .NET 6 SDK and the latest preview of Visual Studio 2022 (which, to date, would be VS 2022 RC). Then the developers should add
NativeFileReference items in a project file, after which they can p / invoke in native code from their .NET code.
Developers can also use libraries, such as NuGet packages, which contain native dependencies after going through a few steps again.
“Files for native dependencies must be built for WebAssembly and packaged in the
browser-wasm architecture specific filesaid Roth. âDependencies specific to WebAssembly will not be referenced automatically and must be manually referenced as
NativeFileReference. Package authors can choose to add native references by include a .props file in the package with references. ”
To detail this, Roth presented a project using SkiaSharp, a cross-platform 2D graphics library for .NET based on Skia’s native graphics library. All this process can be duplicated in a Blazor WebAssembly application example in the SkiaSharp repository.
The other new thing Roth highlighted is an update to minimum API support. He explained in detail the changes affecting parameter binding, OpenAPI, source code analysis, and breaking changes resulting from the renaming of APIs.