locked
Creating webassembly libraries to use with mvc or razor pages RRS feed

  • Question

  • User-1676181112 posted

    hi

    looking at Blazor ( webassembly and server side) it does NOT include the possibility to compile c# libraries to webassembly.

    i.e. i should be able to grab a .net core library and compile it to webassembly file and include the file in the head of any web page( mvc , razor, or even any html page of my own). but i can't see that as part of Blazor.

    am i missing something?

    Saturday, June 20, 2020 10:27 AM

Answers

  • User-474980206 posted

    The is no.c# to webassembly compiler. Mono had an attempt that was used by the blazor prototype. You can probably put a tool chain together. It is a muli-step process

    • transpile c# to c/c++
    • Use a tool like emscripten to compile c/c++ to webassembly.
    • bundle into driver script

    see https://www.mono-project.com/news/2018/01/16/mono-static-webassembly-compilation/

    Blazor uses a different approach. They wrote a .net runtime engine in c/c++ and compiled this to webassembly. Then the webassembly supports loading a .net dll and executing the code.

    note: web assemblies have a lot of restrictions. No threads, no I/o, no o/s calls, no timers, no dom access, and no garbage collector.  They are compute engines. To make a network call or update the dom, they must call into JavaScript. If you need a timer event you call into JavaScript, and JavaScript sets up the timer and call back on the event. 

    to get performance, blazor uses a virtual dom that the webassembly maintains. It then passes this to a JavaScript routine that compares the virtual dom to real dom and makes updates. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 1:27 AM

All replies

  • User-821857111 posted

    i should be able to grab a .net core library and compile it to webassembly file
    That's not how it works. You reference your .NET Core libraries in the normal way and they are downloaded to the client as dlls where they are executed against the WebAssembly version of the .NET runtime.

    Obviously this potentially exposes your IP to anyone who knows how to use a decompiler. If your .NET libraries contain propriety or commercially sensitive code, you can create a Web API method that leverages the library and delivers the result of any operation as JSON.

    Monday, June 22, 2020 2:17 PM
  • User-474980206 posted

    The is no.c# to webassembly compiler. Mono had an attempt that was used by the blazor prototype. You can probably put a tool chain together. It is a muli-step process

    • transpile c# to c/c++
    • Use a tool like emscripten to compile c/c++ to webassembly.
    • bundle into driver script

    see https://www.mono-project.com/news/2018/01/16/mono-static-webassembly-compilation/

    Blazor uses a different approach. They wrote a .net runtime engine in c/c++ and compiled this to webassembly. Then the webassembly supports loading a .net dll and executing the code.

    note: web assemblies have a lot of restrictions. No threads, no I/o, no o/s calls, no timers, no dom access, and no garbage collector.  They are compute engines. To make a network call or update the dom, they must call into JavaScript. If you need a timer event you call into JavaScript, and JavaScript sets up the timer and call back on the event. 

    to get performance, blazor uses a virtual dom that the webassembly maintains. It then passes this to a JavaScript routine that compares the virtual dom to real dom and makes updates. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 23, 2020 1:27 AM