Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: C# library to WebAssembly for usage in JS/TS #16401

Closed
eriklieben opened this issue Mar 17, 2018 · 6 comments
Closed

question: C# library to WebAssembly for usage in JS/TS #16401

eriklieben opened this issue Mar 17, 2018 · 6 comments

Comments

@eriklieben
Copy link

@eriklieben eriklieben commented Mar 17, 2018

Is it also possible to use Blazor to generate a web assembly file that could be used from the browsers JavaScript? Without the Blazor SPA bootstrap, router code, etc?

Like for example: if I have a C# lib that contains string check functions that I use in my dotnet Core webAPI and I want to share that logic (without writing it once in TS and once in C#) with my app that runs a JavaScript SPA so that my app is able to function as PWA and perform the string check logic without sending it back to the server for checking (for example in the case where I am offline and can't ask the server to perform the string check logic because it's unreachable).

Thanks,

Erik

@mattyas
Copy link

@mattyas mattyas commented Mar 17, 2018

Funny, I had the exact same thought today, would it be possible to set up Blazor as a service/component or other way so that you can reach it from JS/TS. The step to move an existing app to Blazor is a big one, the step to reuse the shared logic between frontend and backend is not as big and would be a great benefit.

@grahamehorner
Copy link
Contributor

@grahamehorner grahamehorner commented Mar 17, 2018

I do believe that I've seen a few open source tools that take ILASM and convert them to WASM but I've not personally tried these; I'll hunt a link out for you

@grahamehorner
Copy link
Contributor

@grahamehorner grahamehorner commented Mar 17, 2018

@mattyas
Copy link

@mattyas mattyas commented Mar 17, 2018

It looks like we should be able to talk to Blazer from TS/JS, just need to figure out exactly how to get a function from a .NET Standard dll. https://developer.mozilla.org/en-US/docs/WebAssembly/Exported_functions

@grahamehorner
Copy link
Contributor

@grahamehorner grahamehorner commented Mar 17, 2018

@mattyas have a look at the findMethod & callMethod in the Browse.JS projects

@SteveSandersonMS
Copy link
Member

@SteveSandersonMS SteveSandersonMS commented Mar 19, 2018

Yes, it's totally possible to call .NET from JS (using the findMethod/callMethod APIs that @grahamehorner mentioned), and to avoid doing normal Blazor app rendering (by not putting any logic in your Program::Main method).

There isn't a way of stripping out the Blazor code itself, but TBH, the size of the Blazor libraries is pretty much negligible compared to the much larger overhead of bringing in an entire .NET runtime anyway.

Bear in mind that bringing in a Mono/WebAssembly runtime will add a minimum of about 700KB to your app size, so until we optimise this much further, it's not ideal for most library scenarios. It makes more sense for complete apps, which is the scenario Blazor is focused on.

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@msftbot msftbot bot locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants