我希望能够从JavaScript调用C#代码。单声道项目曾经有一个WASM SDK,您可以从其旧的Jenkins服务器下载,但这不再是公开。现有文档倾向于指向这些构建。 Azure DevOps构建不包括此SDK。我在他们的GitHub帐户上看到的一些消息表明,他们现在专注于WASM的.NET 6。我不想使用大风组件。 .NET 6中有没有办法可以在没有大型UI的情况下构建最小尺寸的WASM二进制文件?
I want to be able to call C# code from JavaScript. The mono project used to have a WASM SDK that you could download from their old Jenkins server, but that is no longer public. Existing docs tend to point toward those builds. The Azure Devops builds do not include this SDK. A few messages I've seen on their Github account indicate that they are now focusing on the .NET 6 for WASM. I do not wish to use the Blazor components. Is there a way in .NET 6 to build a minimally sized WASM binary without the Blazor UI?
发布评论
评论(5)
Nativeaot-llvm,一个实验性C#编译器,不受Microsoft的官方支持,( https://github.com/dotnet/runtimelab/tree/feature/nativeaot-llvm )也可以将C#汇编为C#,而无需任何UI框架要求。关于库的问题编译C#项目到WebAssembly
NativeAOT-LLVM, an experimental c# compiler not official supported by Microsoft, (https://github.com/dotnet/runtimelab/tree/feature/NativeAOT-LLVM) can also compile C# to Wasm without any UI framework requirements. There's a similar question about libraries at Compiling C# project to WebAssembly
Mono Wasm SDK在dotnet/运行时仓库中继续进行。基于旧 packager.exe 的工具已演变为基于msbuild/csproj的解决方案:
https://github.com/dotnet/runtime/tree/main/main/src/mono/wasm
样本:
htttps://github.com/dotnet/runtime/runtime/tree/tree/main /src/mono/sample/wasm
当我尝试在自定义项目中使用代码时的关键问题(不在示例是我们需要引用私有汇编
system.private.runtime.intopservices.javascript.dll
,其中包含在 microsoft.netcore.app.runtime.browser-------------该代码在旧的Mono Wasm SDK中几乎与webAssembly.bindings.dll
。这是我创建的WebGL页面的屏幕截图,基于dotnet/runtime/mono/wasm:
顺便说一句,通过调用C#调用JavaScript函数实现WebGL的性能是坏: https://marcoscobena comle
The Mono WASM SDK is continued in the dotnet/runtime repo. The tooling based on old Packager.exe has evolved into a MSBuild/csproj based solution:
https://github.com/dotnet/runtime/tree/main/src/mono/wasm
samples:
https://github.com/dotnet/runtime/tree/main/src/mono/sample/wasm
The key issue when I trying to use code in a custom project (not within the sample) is that we need to reference a private assembly
System.Private.Runtime.InteropServices.JavaScript.dll
which is included in Microsoft.NETCore.App.Runtime.browser-wasm. The code is almost the same asWebAssembly.Bindings.dll
in old mono wasm sdk.Here is a screenshot of a WebGL page I created, based on dotnet/runtime/mono/wasm:

BTW there is a viewpoint that the performance of implementing WebGL via calling JavaScript functions from C#, is bad: https://marcoscobena.com/?i=wave-engine-web-performance
使用dotjs(nuget)进行C# / JS Interop,而无需大胆。 Vibe建立在上面。它使用另一个库 - 星座(Nuget)来设置Websocket连接。请注意:要使用安全的Websockets(WSS://),在设置为https://时,您需要在星座上添加x509cert。 Websockets使用https://时默认为WSS://。浏览器和客户端之间的所有消息传递已经使用您自己的密钥加密。 DOTJ允许您设置连接关闭时终止的用户特定键。不需要瓦斯。
Use DotJS(nuget) for c# / js interop without blazor. Vibe is built on it. It uses another library - Constellations(nuget) to set up a Websocket connection. Be advised: To use secure websockets( wss://) you need to add an X509Cert to your Constellation when setting it up for https://. Websockets defaults to wss:// when using https://. All messaging between browser and client is already encrypted with your own keys. DotJS allows you to set user specific keys that terminate when the connection closes. No wasm necessary.
是的,绝对有可能。 Blazor对C#/WASM没有垄断,而且很清楚,它将成为最好的长期选择(并且很多证据不是)。
我建议从UNO Wasm Bootstrap开始。 https://github.com/unoplatform/uno.wasm.wasm.wasm.wasm.bootstrap
30编辑 -
更多的证据并不是镇上唯一的游戏,甚至是这里创新的最前沿:
https://visualstudiomagazine.com/articles/articles/Articles/20222/206/06 /29/uno-platform-4-4.4.aspx
Yes it's absolutely possible. Blazor does not have a monopoly on C#/WASM and it's far from clear that it's going to wind up being the best long term option (and a lot of evidence it's not).
I recommend starting with the Uno WASM Bootstrap. https://github.com/unoplatform/Uno.Wasm.Bootstrap
2022-06-30 Edit -
More evidence Blazor is not the only game in town nor even at the forefront of innovation here:
https://visualstudiomagazine.com/articles/2022/06/29/uno-platform-4-4.aspx
Dotnet 7似乎改善了WASM支持。
It seems that dotnet 7 has improved WASM support. More information is provided in https://devblogs.microsoft.com/dotnet/dotnet-7-wasm/