36

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?

12
  • 2
    The UI components. I just want to compile C# and call it from JavaScript and vice versa.
    – eltiare
    Commented Aug 25, 2021 at 22:08
  • 3
    "why?" Is that a serious question? Commented Aug 26, 2021 at 1:21
  • 2
    Unfortunately, probably is. A lot of people don't believe in framework diversity, even if there are plenty of legitimate reasons for it.
    – eltiare
    Commented Aug 28, 2021 at 14:44
  • 2
    Blazor is a tool meant to replace JS with C#. Web Assembly is meant to run along side of JS, not replace it. Blazor would not be to C# and Web Assembly to what Emscripten is to C/C++ and Web Assembly. Also, Blazor advertises to write UI Components in C#. Google engineers state to not write your components in Web Assembly.
    – dman
    Commented Dec 29, 2021 at 3:09
  • 2
    @dman I agree with this, especially "Google engineers state to not write your components in Web Assembly." Blazor's biggest flaw is doing WAY too much performance-sensitive stuff, most notably DOM building and diffing, in the C# world then transferring it all over to JS through very inefficient interop. Thus Blazor apps with any significant complexity are not really usable, even after AOT. Building your UI natively and calling into C# as needed is currently the only viable approach if you expect people to actually use and pay for your app. Unfortunately MS doesn't officially support this. Commented Jan 21, 2022 at 13:33

4 Answers 4

17

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

12
  • 1
    I was really hoping that .NET 6 would just support this. But it looks like a good lead. Thank you.
    – eltiare
    Commented Aug 27, 2021 at 17:52
  • 6
    Yeah Microsoft doesn't seem to be interested in WASM except in the context of Blazor. Very strange and annoying that they killed the Mono SDK. Plus, despite a huge outpouring of support for a WASM/Web target for MAUI, they've stayed completely silent on it. It really is inexplicable. But the guy behind Uno is super smart and helpful and really believes in combining C#+WASM so he is very open to projects like this that don't use Blazor OR Uno. Follow his directions to the letter and you will be able to get it working I'm sure. Commented Aug 27, 2021 at 18:46
  • 1
    @CharlesChen All of those projects seem oriented around wasm used as servers/microservices, not wasm for in-browser js use.
    – Mike Asdf
    Commented Mar 29, 2022 at 20:59
  • 1
    I want to point out that the great thing about Uno.Bootsrap.Wasm is it is independent of the Uno Platform. If you don't want to learn a new UI framework like Uno or Blazor, then Uno.Bootstrap.Wasm lets you compile and load a client side .NET WebAssembly in a traditional website/browser context. I've used it to build things like WASM JQuery wrapper so I can take a traditional ASP.NET MVC site and implement the client side UI logic in C# instead of Javascript.
    – AaronLS
    Commented Sep 15, 2023 at 2:04
  • 1
    @EmperorEto Just spent couple hours getting it to work. The Uno Bootstrap documentation seems like it falls behind the Uno Platform documentation. I needed to add an inspectUri to launchsettings.json for it to work.
    – AaronLS
    Commented Sep 16, 2023 at 2:44
7

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

5

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

2
5

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: enter image description here

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

1
  • Nice find. DOM manipulation in WASM is going to be slow for the foreseeable future but plans are in the works to bring it up to par with JavaScript.
    – eltiare
    Commented Jan 30, 2022 at 16:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.