如何使用单声道WebAssembly运行在浏览器中简单的.NET方法?

问题描述 投票:1回答:1

比方说,我在服务器上有一个.NET的DLL文件,有这个简单的类:

public static class C {
    public static int Add(int a, int b) => a + b;
}

我想用Mono的WebAssembly支持调用浏览器C.Add。 (假设我可以下载DLL到浏览器中,例如与fetch

问题:

  1. 需要什么样的.js / .wasm文件单和我在哪里得到这些?
  2. 一旦一切都装,实际上,我怎么从JS调用C.Add

我检查NPM,但我还没有发现单WASM那里。

注:我已经有一个DLL,所以我感兴趣的WASM IL解释,而不是WASM AOT版本。

.net mono webassembly
1个回答
6
投票

这里是我的发现。

Obtain Mono WASM

让我们把解压后的文件夹WASM-SDK

注意:您可以跳过下面的步骤,如果你运行packager.exe在单文档描述,但我想在这里说明的手动方法更好地理解。

Prepare .NET dlls

将下面的DLL您的网站根目录下(让managed文件夹下说):

  • 包含class C主要DLL,让我们把它app.dll
  • BCL的相关性,在这种情况下,你只需要: WASM-SDK\wasm-bcl\wasm\mscorlib.dll WASM-SDK\wasm-bcl\wasm\Facades\netstandard.dll WASM-SDK\framework\WebAssembly.Bindings.dll

Prepare Web files

  1. 您的网站根目录下复制从mono.js mono.wasmWASM-SDK\release
  2. 注册您的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>
  1. 如果使用IIS,请确保没有为application/wasm扩展.wasm MIME类型寄存器。

All done

现在,一旦你打开你的HTML你应该看到C.Add: 3记录在浏览器控制台。

© www.soinside.com 2019 - 2024. All rights reserved.