How do I use Mono WebAssembly to run a simple .NET method in browser?(如何使用 Mono WebAssembly 在浏览器中运行简单的 .NET 方法?)
问题描述
假设我在服务器上有一个 .NET dll 文件,它有一个简单的类:
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;
}
我想使用 Mono 的 WebAssembly 支持在浏览器中调用 C.Add.
(假设我可以将 dll 下载到浏览器中,例如使用 fetch)
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)
问题:
- Mono 需要哪些 .js/.wasm 文件,我从哪里获得这些文件?
- 加载完所有内容后,如何真正从 JS 调用
C.Add?
我检查了 npm,但我没有在那里找到 Mono WASM.
I checked npm but I haven't found Mono WASM there.
注意:我已经有一个 dll,所以我对 WASM IL 解释器感兴趣,而不是 WASM AOT 构建.
Note: I already have a dll, so I'm interested in WASM IL interpreter and not WASM AOT build.
推荐答案
这是我找到的.
- 步骤如下所述:docs/getting-started/obtain-wasm-sdk.md
- 简短的总结:您必须下载并解压缩 从 Jenkins 构建
让我们调用解压后的文件夹WASM-SDK.
Let's call the unpacked folder WASM-SDK.
注意:如果按照 Mono 文档中的描述运行 packager.exe,则可以跳过以下步骤,但我想在此处描述手动方法以便更好地理解.
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.
将以下 dll 放在您的站点根目录下(比如说在 managed 文件夹下):
Put the following dlls under your site root (lets say under managed folder):
- 包含
class C的主dll,我们称之为app.dll - BCL 依赖,在这种情况下你只需要:
- Main dll that contains
class C, let's call itapp.dll - BCL dependencies, in this case you only need:
WASM-SDKwasm-bclwasmmscorlib.dllWASM-SDKwasm-bclwasmFacades etstandard.dllWASM-SDKframeworkWebAssembly.Bindings.dll
WASM-SDKwasm-bclwasmmscorlib.dllWASM-SDKwasm-bclwasmFacades etstandard.dllWASM-SDKframeworkWebAssembly.Bindings.dll
- 从站点根目录下的
WASM-SDK elease中复制mono.js和mono.wasm - 注册你的
Module并导入mono.js:
- Copy
mono.jsandmono.wasmfromWASM-SDK eleaseunder your site root - Register your
Moduleand importmono.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="bW9uby5qcw=="></script>
- 如果使用 IIS,请确保有一个
application/wasm用于.wasm扩展的 mime 类型寄存器.
- If using IIS, make sure that there is a
application/wasmmime type register for the.wasmextension.
全部完成
现在,一旦您打开 HTML,您应该会在浏览器控制台中看到 C.Add: 3.
这篇关于如何使用 Mono WebAssembly 在浏览器中运行简单的 .NET 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 Mono WebAssembly 在浏览器中运行简单的 .NET 方法?
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
