Is it possible to compile existing C# code to WebAssembly (.wasm) so that no or nearly no code changes have to be done?
Do I have to use Blazor for it or are their other possibilities?
-
5Currently (.NET 5.0), Blazor does not compile your code to WebAssembly. Instead, it uses a .NET runtime, which has been compiled to WebAssembly, that interprets the IL of your code.pschill– pschill2021-01-26 12:50:33 +00:00Commented Jan 26, 2021 at 12:50
-
Thanks a lot, so I can use existing C# code?Janine– Janine2021-01-27 06:46:16 +00:00Commented Jan 27, 2021 at 6:46
2 Answers
.NET 8 has improved this further.
Use .NET 7. This support for compiling C# to wasm is now officially added even outside of the Blazor ecosystem.
https://devblogs.microsoft.com/dotnet/use-net-7-from-any-javascript-app-in-net-7/
Full .NET 7 release notes: https://devblogs.microsoft.com/dotnet/announcing-dotnet-7/
While still slightly raw around the edges, this is supposed to get buttoned up and refactored significantly for .NET 8, according to the first comment here.
2 Comments
Is it possible to compile existing C# code to WebAssembly (.wasm)
No, there is no compiler (yet). But
so that no or nearly no code changes have to be done?
That is easy, you can simply add .net DLLs (packages) to your project.
Just make sure that the code makes sense in a Browser, ie no Threading, no I/O except HttpClient etc.
Do I have to use Blazor for it or are their other possibilities?
For now there is only Blazor.