Can Blazor WebAssembly code call functions from another WASM module directly or is a JavaScript bridge required?
OpenCV has been built for WASM, as OpenCV.js. Is there a way of exporting the function definitions from the OpenCV WASM implementation and using them directly from the Blazor code? Is the best alternative to use JS Interop and have JS functions that call the OpenCV functions?
In essence, you can write C# code where you'd previously use JavaScript, and Blazor will compile that C# to WebAssembly to allow it to run in the browser. However, I was a little surprised to find that when I tested the speed of it versus the application written in JavaScript, it was noticeably slower.
Blazor Server apps have direct access to server and network resources where the app is executing. Because Blazor WebAssembly apps execute on a client, they don't have direct access to server and network resources.
Blazor WebAssembly is a single-page app (SPA) framework for building interactive client-side web apps with . NET. Blazor WebAssembly uses open web standards without plugins or recompiling code into other languages. Blazor WebAssembly works in all modern web browsers, including mobile browsers.
Ahead-of-time (AOT) compilation. Blazor WebAssembly supports ahead-of-time (AOT) compilation, where you can compile your . NET code directly into WebAssembly. AOT compilation results in runtime performance improvements at the expense of a larger app size.
Blazor WebAssembly (WASM) apps executes directly in the browser on a WebAssembly, just like JavaScript. Blazor WebAssembly apps are provided with the following things:
When creating a Blazor Wasm site you can choose this option in Visual Studio by selecting these options: Both of these create a solution with a Blazor Wasm client app, ASP.NET Core Server app, and a shared (optional) library project for sharing code between the two (like models or things like that).
What you need to do is bind your service and client together like this: First be sure your 3rd party api support CORS or you will not be able to call them. On blazor WASM httpclient just uses JavaScript to make network calls, and is restricted by the browser rules.
Blazor WebAssembly apps can be deployed to Azure App Services on Windows, which hosts the app on IIS. Deploying a standalone Blazor WebAssembly app to Azure App Service for Linux isn't currently supported. A Linux server image to host the app isn't available at this time.
This should be achieved much more easily with .NET 6 as Blazor now supports native dependencies. You can compile the OpenCV C++ or OpenCV.js to wasm
using emscripten
and add a native file to your csproj
as shown below.
<NativeFileReference Include="path-to-output-file" />
You can then go ahead and P/Invoke into the native code. In this YouTube video, Steve Sanderson demos a very similar scenario. You can also check out the docs on Native Dependencies in Blazor for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With