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="mono.js"></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 方法?
基础教程推荐
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
