比方说,我在服务器上有一个.NET的DLL文件,有这个简单的类:
public static class C {
public static int Add(int a, int b) => a + b;
}
我想用Mono的WebAssembly支持调用浏览器C.Add
。
(假设我可以下载DLL到浏览器中,例如与fetch
)
问题:
C.Add
?我检查NPM,但我还没有发现单WASM那里。
注:我已经有一个DLL,所以我感兴趣的WASM IL解释,而不是WASM AOT版本。
这里是我的发现。
让我们把解压后的文件夹WASM-SDK
。
注意:您可以跳过下面的步骤,如果你运行packager.exe
在单文档描述,但我想在这里说明的手动方法更好地理解。
将下面的DLL您的网站根目录下(让managed
文件夹下说):
class C
主要DLL,让我们把它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
mono.wasm
和WASM-SDK\release
Module
和进口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
扩展.wasm
MIME类型寄存器。现在,一旦你打开你的HTML你应该看到C.Add: 3
记录在浏览器控制台。