Is there a Valid way to run node modules from C# Class - c#

Is there a Valid way to run node modules from C# Class ?
There are some options which I know
Use JS/TS to execute node module and host it in node API. Call that node API from c#.
Use command prompt/Powershell to execute node script and run powershell process from c#.
Use thirdparty nuget package like jerring or Jint
1st option is straight forward but I don’t like the approach as it seems like calling another api from one api.
2nd option is feasible when running on windows vm and also creation of additional process is a burden on processor.
3rd option is preferrable but these libraries do not have full support for ES6.
Earlier there was aspnetcore.JavaScript service package but that seems to be deprecated
Please suggest best option or any new option if available.

You can try this for running Javascript from C# https://andrewlock.net/running-javascript-in-a-dotnet-app-with-javascriptengineswitcher/

Related

Best way to call and access powershell script in ASP.NET Core project

I have a PowerShell script which I have to call in an ASP.NET Core Web API solution.
What is the best way to use the PowerShell script in an ASP.NET Cre Web API solution?
Should I add the PowerShell script as a string constant in a class or add a PowerShell script in the project solution (source control) and then maybe using AppContext.BaseDirectory get the path and then use it?
I am sorry that this is not a programming question since I just needed a suggestion of what will be the correct way to implement the same.
Thanks
I think the method you mentioned is feasible, but not necessarily the best. The reason is that when we find the script file, we let the server execute the script, and we cannot quickly locate the events or exceptions during execution.
It seems that we can use some class libraries (such as Microsoft.PowerShell.SDK), we can store the script content in the database, and execute these scripts in the asp.net core program. It should be possible to get status or return information at every step of execution, which may be more friendly.
Related Blog: How to run PowerShell Core scripts from .NET Core applications

Is it bad practice to let Node.js talk to a REPL child process?

I have a Node.js process that spawns a child REPL process. This child process speaks C# and is able to load DLL files dynamically. I send input to the REPL using either methods or sometimes using a Proxy(). The stdout of the REPL is sent back to my object and then caught using a common obj.on(data, function) implementation. When I invoke a method on the object, it will pass it to the child process stdin.
This all works like a charm, I can load any .NET or third party assembly and expose its types and methods to Node. Although extremely effective, it just instinctively feels very ugly do to it this way.
It does not have to work other platforms besides Windows.
Is this 'bad practice'? If so, why is this so? What is bad about it? How would you do it better?
edit: I am not looking for an alternative way, but I would like an answer to my questions that I now made bold.
If you need interop between node and .NET in either direction, there is the edge.js project, which may be the kind of thing you're looking for. Currently accessing CLR from node is available cross-platform (Windows, Linux, OS X).

Call Python from .NET

I have some code written in Python which can not be transferred to a .NET language. I need to call one of these functions from my .NET WinForms application.
Now, I do it by starting the Python script as a separate process and pass parameters to it as command line arguments. It works, but I don't really like this solution. I'd like to improve it to a better one.
Is there any better way to call a function of a .py script from a .NET application? What is the best way to do it?
Note: IronPython is NOT an option for this Python script
This might be a lot more work than launching the Python process, but here's an alternate solution.
You can embed Python into another program. The API is for C and Interop from .NET will probably be a major pain. If you're into a bit of a safer way to handle the native Python API, you can look into Boost.Python, which, among its less advertised features, has support for embedding.
With these tools, you can write a C++ managed DLL that uses Boost.Python to load the Python interpreter and execute any Python script. Thus, you can execute any Python code directly in the hosting process, eliminating the use of an external process and any form of IPC.
Edit: AFAIK, all you have to add to your installation procedure is the deployment of the Python DLL and Boost.Python DLL.
Besides the COM option, you could make your Python script instantiate a xmlrpc server -
it is quite transparent and you never have to deal with "xml" on your own code.
Then, on .net side, you simply connect to your python app via xmlrpc - if there is no suitable way to do that in C#, just write a client function in IronPython.
The SimpleXMLRPCServer example on Python documentation is enough for that:
http://docs.python.org/library/simplexmlrpcserver.html
I think you need to re-evaluate Carlos' answer.
See the section Implementing COM Objects with Python in Mark Hammond's book Python Programming on Win32.
You should be able to create a COM object, then have .Net interact with it.
From the book the following will create a COM server with a single method.
# SimpleCOMServer.py - A sample COM server - almost as small as they come!
#
# We expose a single method in a Python COM object.
class PythonUtilities:
_public_methods_ = [ 'SplitString' ]
_reg_progid_ = "PythonDemos.Utilities"
# NEVER copy the following ID
# Use "print pythoncom.CreateGuid()" to make a new one.
_reg_clsid_ = "{41E24E95-D45A-11D2-852C-204C4F4F5020}"
def SplitString(self, val, item=None):
import string
if item != None: item = str(item)
return string.split(str(val), item)
# Add code so that when this script is run by
# Python.exe, it self-registers.
if __name__=='__main__':
print "Registering COM server..."
import win32com.server.register
win32com.server.register.UseCommandLine(PythonUtilities)
The book goes on to say ".. you can do this by executing the code as a normal Python script. The easiest way to do this is to open the source file in PythonWin and use the Run command from the File menu. "
I think you need the ActivePython distribution from Activestate to do it.
See this question Consuming Python COM Server from .NET
It works, but I don't really like this solution, I'd like to improve it to a better one.
No, AFAIK there isn't a better solution, especially if IronPython is a no-no for you. So you could still keep this as a temporary workaround while waiting for the script to be migrated to .NET or until you find that someone already wrote a library on .NET that provides you with similar functionality.
Create a COM .dll from a .py script and use Interop in your .NET code.
Have a look here: http://docs.python.org/faq/windows.html
PythonNet should help with this one. It enables you to call python code from C#.
A cleaner way is to expose the python script via a Flask REST API and consume that from your .NET Application. Don't forget to put proper authentication in place.

Calling a running C# application from VBA

I have some VBA code that needs to talk to a running c# application.
For what it's worth, the c# application runs as a service, and exposes an interface via .net remoting.
I posted a question regarding a specific problem I'm having already (From VB6 to .net via COM and Remoting...What a mess!) but I think I may have my structure all wrong...
So I'm taking a step back - what's the best way to go about doing this?
One thing that's worth taking into account is that I want to call into the running application - not just call a precompiled DLL...
In the past, one way I accomplished something similar was with Microsoft Message Queueing. Both languages/platforms can read/write to a queue.
In my scenario, we had a legacy Access database that we had to maintain. We wanted to migrate away from it and replace it with a more robust .NET solution. To get real time data out of the current system into the new system, we added VBA code to write data to a message queue. Then we wrote a C# windows service to process that data in the new system.
I'm not entirely sure of what you're doing, so this may not be a fit, but I thought I'd mention it.
I've come up with a solution using my original structure...
That is, the VBA application calls a COM wrapper application that translates all of the types from .Net to COM safe types. This wrapper then calls the main service using .net remoting.
The problem I was having was that the common dlls between the wrapper and the service needed to be in the C:\Program Files\Microsoft Office\Office12 folder (along side msaccess.exe).
While I was using the AssemblyResolve method to provide the dlls at runtime, this wasn't working...So for now I'll just have to have the dlls copied to the folder - a far from elegant solution, but at least the communication is working for now.

.NET scheduler that runs assemblies?

This may already exist I hope.
I want to create a Windows service that schedules tasks (much like the Windows o e) but it would run C# assembles dynamically from a local folder (schedule rules via a XML file for that task) so I don't have to create a Windows service or console application every time I want some automation code but can build an assembly, upload it to a directory with a XML config for the timing and it will run/ be detected b a folder watcher.
Does this already exist or will I have to build it?
It's probably starting to look like I'm promoting this.... Try Quartz.Net
http://quartznet.sourceforge.net/
Sounds like a job for Windows PowerShell.
I assume you want to automate the invocation of code in any arbitrary .NET assembly, even those for which you don't have the source code, rather than a plug-in approach with a well defined interface. If so you'll be building a generic service that can watch the config file, which itself must contain all of the details on which assemblies to load, which types to instantiate, which properties to set, which methods to call and their arguments, etc. You might perform all of this work via reflection, or Unity and/or XAML may be of help in constructing the objects.
Depending on how sophisticated you want to get, your config file will end up looking very much like C# code, so it may not be as worthwhile an exercise as you think.
Check out the .NET namespace doc at http://www.jamsscheduler.com/doc/DevelopersGuide/Default.html
It's a .NET based commercial job scheduling system but I use the free Developer's Edition.

Categories

Resources