Running Javascript along side C# using Blazor in headless browser - javascript

I'm trying to build a .NET core application that execute JavaScript, this JavaScript also need to call C# functions on CLR objects as well ,I used JINT (https://github.com/sebastienros/jint) but it has one majeure disadvantage which i couldn't debug any JavaScript code.
So thinking for alternative that using CEF sharp that will run Blazor to execute javascript alongside C#. is it possible ?
Thanks!

You can use JSInterop in blazor to communicate between JS and C#.
Read more here for calling JS from C#:
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-5.0
And here for calling C# from JS:
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-5.0

Related

Create WebAssembly using c#

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.

How to communicate from JavaScript to filesystem

I am making a Windows application in Visual Studio 2010 by embeding a webbrowser object loading a local file.
Now I want the web page to read and write from and to the filesystem from JavaScript.
I have done the same in a HTA just calling simple VBS functions for read and write files.
How do I do in .NET 4.0?
You can fill data using provided methods.
webBrowser1.Document.GetElementById("myTextbox").SetAttribute("value") = "my_value" ;
and it is also possible to Invoke javascript functions as well. So that is how you can do it.

Can .NET DLLs be created using Javascript or JScript.NET?

I'm creating a video game that has single player and multiplayer. The single player is done in C# (Unity), and want to use Gamesparks BaaS that works on Javascript (NodeJS) for their server code.
Since I want the multiplayer server to be authoritative, I need the server code to run Javascript and the client will run C#, which means duplicated code
But, if I can create .NET dlls in Javascript I can reduce code duplication heavily by using those libraries in Unity and using the Javascript code on the server.
Thanks!
If you want to use javascript (and by javascript I mean nodejs), along with c# and communicate between those two, you can use electron-edge. It helps you to run c# code using nodejs through Electron API.
Here is the link of their Github url: Electron-Edge-documentation
You can also interact with dlls, using an creating your application as an electron app. You refer this question where you will find some more details regarding it.

Selenium with C# and Javascript

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.

How to execute Javascript code in a WinRT C# application?

I am writing a WinRT application in C#.
I would like to execute some Javascript code. An entire WebView is not needed in my case, I just need to interface with a Javascript virtual machine. The C# code would call Javascript functions and would export some objects to the Javascript side.
I know there is JsRT, the C API of Chakra, the jsvm used by Internet Explorer. But JsRT, as far as I know, is not available to WinRT. Other virtual machines are not allowed under WinRT.
So, the question is: is it even possible to call Javascript code from C# code? If not, waiting for the release of JsRT for WinRT is the only option?
It's possible to call JavaScript from C# but not without a WebView control.

Categories