简体   繁体   中英

C# WASM without Blazor

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?

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

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

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 as WebAssembly.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

It seems that dotnet 7 has improved WASM support. More information is provided in https://devblogs.microsoft.com/dotnet/dotnet-7-wasm/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM