I went through below article
https://medium.freecodecamp.org/get-started-with-webassembly-using-only-14-lines-of-javascript-b37b6aaca1e4
and very impressed that we can use c++ code in javascript using web assembly.
Do we have any option to create such web assembly using c# where we can create web assembly and use in javascript like angular or react.
went through
https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/blazor/get-started?view=aspnetcore-3.0&tabs=visual-studio
But that does not look like creating an assembly that can be used in a separate angular only project with by importing
Thanks
From msdn:
JavaScript interop
For apps that require third-party JavaScript libraries and browser APIs, Blazor interoperates with JavaScript. Components are capable of using any library or API that JavaScript is able to use. C# code can call into JavaScript code, and JavaScript code can call into C# code. For more information, see JavaScript interop.
[https://learn.microsoft.com/en-us/aspnet/core/client-side/spa/blazor/?view=aspnetcore-3.0][1]
Ithink looking to Blazor source code can help you to.
I think what you're looking for isn't Blazor, because it's a complete UI framework for ASP.NET client/server stuff using SignalR, that uses WASM at the client side. Maybe too much for your purposes, even if you're able to use any JavaScript framework together with Blazor, too.
But you're looking for a simple way to create just a WASM that exports methods to JavaScript that you can write using C#, right? Well, then I suggest you to have a look here:
https://itnext.io/run-c-natively-in-the-browser-through-the-web-assembly-via-mono-wasm-60f3d55dd05a
It seems the Mono way is working as you'd expect: You write methods in C#, compile a WASM and then you're ready to load and call them from any JavaScript client app, and you don't have to deal with ASP.NET stuff at all.
Compared to a WASM that has been created using lower level C++, you'll have a big bunch of DLLs for the Mono runtime, that need to be loaded to the client browser (!). That's a huge overhead, if you plan only a small feature set to be exported by the WASM. The best argument for creating WASM using Mono for me is, that I can use my existing codebase with all the algorithms and business logic for a really complex app, so I don't have to write and maintenance the same code twice in different languages.
Related
Im angular developer.
In our front & back exists some magic calculation methods.
Classes same, but when anyone find bug in calculation need to fix it in two different projects.
maybe there is a way to create a generic codebase (maby function) that can be converted to js(or ts) & java and update two libraries based on the two results obtained
You could try to use kotlin.
Kotlin transpiles to JavaScript and also compiles to java bytecode.
However, you can only access kotlin utilities and neither access java or JS/TS types if you want to use the code in both java and ts/js code but you can use the kotlin stdlib.
But if it really is just a calculation, you may not need java/js specific classes/functions.
However, as VLAZ mentioned in the comments, you should consider doing the calculation only once in the backend.
Setting this up in IntelliJ
You can create such a project in IntelliJ by sekecting Kotlin in the New Project Window and using the project template Library.
Make sure you have the targets common, jvm and js. Since you didn't say you would do native stuff, you don't need the native target.
You can then use the kotlin library in both JavaScript (e.g. Angular) and Java projects as a dependency.
From a Java project, you can reference KOTLIN_PROJECT/build/classes/kotlin/jvm/main (this directory contains compiled Java classes).
From a JavaScript (e.g. Angular) project, you can reference KOTLIN_PROJECT/build/js/packages/kotlinToJavaAndJS.
As a proof of concept, I have made this repository on GitHub.
I know that it's possible to transform avdl to series of avsc files using java tools provided by Apache.
But despite the website lists plenty of implementations on different languages too, including JS, it seems to be that there is no support for avdl -> avsc conversion in these.
What would be your recommendation how to perform this conversion in pure JavaScript? Does such library even exists or we're forced to go through pure Java implementation always?
Java seems to be the only language that they implemented the compilation from avdl to avsc. The easiest route is probably just to have the avro-tools.jar somewhere and then have your JS code call out to that in some sub process to compile the schemas.
The other option would be to re-implement the IDL compiler in JS. I wouldn't do that, but the Java implementation is pretty much all contained within https://github.com/apache/avro/blob/master/lang/java/compiler/src/main/javacc/org/apache/avro/compiler/idl/idl.jj if you wanted to take a look.
We have a large ecosystem of Javascript websites, actually Angular, that we don't plan to rewrite in c# any time soon. So the goal here is to be able to use a vendor dll in our javascript to add new features. This is a proprietary system, we don't have any alternative, either we use their dll, either we don't have the feature. I'm putting a lot of hope in webassembly here because this looked like the silver bullet to use that dll without having to rewrite the whole project in a new language.
Problem: all the examples I can find are more about using Blazor to write a website or call javascript from Blazor, I can't find anything to include some ad-hoc C# code into an existing project. I would have thought it would be a great use case though because being able to leverage C# threadpool on a webpage sounds pretty good to me!
Anyone has done something similar or know some examples/tutorials I could follow?
As Tuan says Angular and C# don't really live together in the way you suggest.
I would say there are 2 separate approaches here.
Have an C#.NET MVC app but adjust the routing so the Angular App Handles some pages and the .NET app handles others. This is OK but there are so many pitfalls such as the fact that you can not share bundled CSS and JS or maintain the structure of your Angular controllers (amongst others).
A better way would be keep your lovely Angular app the way it is but have a separate Web API application/project and use the angular app to call the dll (reference in the project) within the correct context as a REST API (via a simple POST or GET call).
It's not too clear exactly what you want to do when you say "... able to use a vendor dll in our javascript to add new features ..." but you can find info on writing and implementing C# as a Web API in .NET core here
https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&tabs=visual-studio
I am a little curious about how javascript frameworks work. Web development isn't really my area of expertise (I'm more of a c/c++ guy), but do javascript frameworks get translated into vanilla javascript?
Upon inspection of website source, it seems like it is mostly just standard javascript. Do these javascript engines just translate code into javascript on the server side?
Yes, most of JavaScript Frameworks translates the code you write to vanilla JavaScript, however, this does not happen on the Server Side, that would be really slow (Server side code is used to check databases, serve files, authenticate, etc.). This process of translation is done in compilation time (Although it is translation). (Just like when you compile c++ code into binary).
When it's source code to source code like JavaScript and React (JSX) to Vanilla JavaScript (JS), it's translation. When it's source code to binary like C++ source code to an executable (.exe) is compilation.
After you're done writting your JavaScript code with frameworks, you most translate it to Vanilla JavaScript (if you also used other uncommon languages to write styles, you must translate them too, like SASS instead of CSS). It is also common to minify it, so it can load faster.
All this is mainly done by tools like webpack.
When your site is up and running, we can say that is run time.
Looking at the fact that they were written in js they would be resolved to js before running and as Robin said they are executed on client side except Node which is a runtime environment and not a framework
In our project we are trying to figure out which approach would be better for testing from the below
1. Selenium with C#
2. Selenium with Java Script
I am able to find that C# require Selenium libraries and NUnit framework. However, is it possible to use MS Test instead of NUnit framework? We are using Visual Studio 2013.
Secondly, for JavaScript, I found that we need Standalone server to be run to execute the scripts. Is there any good framework available to implement selenium using Javascript?
If you are comfortable with javascript why not use Java with Eclispe and TestNG. This would not require a standalone server implementation to test.
You can consider the NUnit framework to be just a tool. You have lots of options with regards to C#: Xunit, Nunit, MSTest... Selenium is awesome. The question is more C# vs Javascript and which is better suited to your company. I worked at a place where everyone used C#, so naturally using C# for testing meant a lot of external support. You can also call Javascript code in C# if needed via Selenium. I do this in a few rare instances. For Javascript, you have specflow, protractor, and Jasmine which work well together.