Let's say I have a .NET dll file on the server, that has this simple class:
public static class C {
public static int Add(int a, int b) => a + b;
}
I want to invoke C.Add
in browser using Mono's WebAssembly support.
(assume that I can download the dll into browser, e.g. with fetch
)
Questions:
C.Add
from JS?I checked npm but I haven't found Mono WASM there.
Note: I already have a dll, so I'm interested in WASM IL interpreter and not WASM AOT build.
Here's what I found.
Let's call the unpacked folder WASM-SDK
.
Note: you can skip following steps if you run packager.exe
as described in Mono docs, but I want to describe the manual approach here for better understanding.
Put the following dlls under your site root (lets say under managed
folder):
class C
, let's call it app.dll
WASM-SDK\wasm-bcl\wasm\mscorlib.dll
WASM-SDK\wasm-bcl\wasm\Facades\netstandard.dll
WASM-SDK\framework\WebAssembly.Bindings.dll
mono.js
and mono.wasm
from WASM-SDK\release
under your site rootModule
and import mono.js
:<script>
window.Module = {};
window.Module.onRuntimeInitialized = () => {
const config = {
vfsPrefix: "managed",
deployPrefix: "managed",
enableDebugging: 0
};
const assemblies = [
'app.dll',
'mscorlib.dll',
'WebAssembly.Bindings.dll',
'netstandard.dll'
];
MONO.mono_load_runtime_and_bcl(
config.vfsPrefix,
config.deployPrefix,
config.enableDebugging,
assemblies,
() => {
Module.mono_bindings_init("[WebAssembly.Bindings]WebAssembly.Runtime");
const add = Module.mono_bind_static_method("[app] C:Add");
// ⬇️ This is what calls C.Add():
console.log('C.Add:', add(1, 2));
}
)
};
<script>
<script async src="mono.js"></script>
application/wasm
mime type register for the .wasm
extension.Now once you open your HTML you should see C.Add: 3
logged in the browser console.
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