Is it possible to productionize Python code in a .NET/C# environment without installing Python and without converting the Python code to C#, i.e. just deploy the code as is?
I know installing the Python language would be the reasonable thing to do but my hesitation is that I just don't want to introduce a new language to my production environment and deal with its testing and maintenance complications, since I don't have enough manpower who know Python to take care of these issues.
I know IronPython is built on CLR, but don't know how exactly it can be hosted and maintained inside .NET. Does it enable one to treat PYthon code as a "package" that can be imported into C# code, without actually installing Python as a standalone language? How can IronPython make my life easier in this situation? Can python.net give me more leverage?
As I mentioned in the comments, the right and better way to do it is to create Restful services over your Python code and make http-requests from the C# code. I don't know how much you know about web-frameworks in Python but there are tons of them that you can use. For your need, I would suggest Flask which is light-weight micro web-framework to create Restful web services.
This is very simple Flask web-service for the example: (you can check a running version of it here, I hosted it on pythonOnEverywhere)
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello from Flask!'
#app.route('/math/add/<int:num1>/<int:num2>')
def add(num1, num2):
return '%d' % (num1+num2)
This simple service, adds two number and returns the sum of them.
And C# Code:
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
public class Program
{
// Always use HttpClient as a singleton object
public static HttpClient _httpClient = new HttpClient() { BaseAddress = new Uri("http://al1b.pythonanywhere.com") } ;
public async static Task Main()
{
var num1 = 1;
var num2 = 4;
Console.WriteLine("Making http-request wait ...\r\n");
var mathAddResult = await _httpClient.GetAsync($"/math/add/{num1}/{num2}");
// 200 = OK
if(mathAddResult.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine(await mathAddResult.Content.ReadAsStringAsync());
}
}
}
The output:
Making http-request wait ...
5
The running version of code above is now runnable on .NET Fiddle.
TL;DR:
For understanding and learning Flask, take a look on its documentions. (It's short and well). I'm sure you will have complex web-services, such as accepting complex or pocco objects as your web-service inputs and returning complex objects (as json) as web-serivce results.
In that case you need to know how Flask jsonify works, This link will tell you how.
Ok, on the other hand, in your C# application you will have those complex objects and scenarios as well. You need to know how to serialize, deseriaze and etc.
Microsoft did a great job for its tutorials here:
https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
and
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8
If you don't want to introduce a new language to your production environment you should keep all of your code C#, instead of introducing python.
With that said, you don't need to 'install' the python runtime, but you would need to have a runtime available. If that involves installing a nuget package, some mono implementation, or whatever, you are going to rely on some dependency to interpret the python commands.
Here's an article, that I believe answers your question.
How to use a packed python package without installing it
IronPython is limited compared to running Python with C based libraries needing the Python Interpreter, not the .NET DLR. I suppose it depends how you are using the Python code, if you want to use a lot of third party python libraries, i doubt that IronPython will fit your needs.
What about building a full Python application but running it all from Docker?
That would require your environments to have Docker installed, but you could then also deploy your .NET applications using Docker too, and they would all be isolated and not dirty your 'environment'.
There are base docker images out there that are specifically for Building Python and .NET Project and also for running.
I really do not understand the question. You do not want to install python, you do not want to introduce it to your system since it has an additional maintance cost and your team lacks the basic knowledge of it. Still, you want to "productionize" it without anyone knowing.
My assumption you want to have a scripting language, and for some reason you decided on python. My answer is based on this assumption.
IronPython is why we have dynamic keyword and DLR in .net world. You can host it you application and make it compile some python code to .net compatible types.
IronPython's own documentation mentions it
https://ironpython.net/documentation/dotnet/dotnet.html#accessing-python-code-from-other-net-code
Also there is an MSDN blog post showing how to do that.
https://blogs.msdn.microsoft.com/seshadripv/2008/06/30/how-to-invoke-a-ironpython-function-from-c-using-the-dlr-hosting-api/
Hope it helps
If you must run some Python code from a .NET and you don't want to ship Python with the app then you should just compile the Python code down to an EXE using something like https://cython.org/
You can pass variables in to it on the command line and receive answers back from a Python function by writing to the console from Python and capturing the output of the process you spawned from .NET. This way it would work just like calling any other Sub or Function, the only difference is each procedure is a new process to .NET. This could get really complicated if you want to pass complex objects back and forth as you would have to unwrap and wrap them as they flow, as text, between the components.
Ultimately, you would be better off just rewriting your Python code in .NET and not going down this path at all. Use a converter tool like https://github.com/uxmal/pytocs to convert all the Python code to C#. It likely won't be perfect but it will give you a codebase to work on replacing the Python functions you want to use. It will take you less time to clean up the translated procedures than it would to try to maintain both languages and inter-op functions between them.
Related
I have a python code. I need to execute the python script from my c# program. After searching a bit about this, I came to know that there is mainly two ways of executing a python script from c#.
One by using 'Process' command and
the other by using Iron Python.
My question might seem dumb, is there any other way through which I can execute a python script? To be more specific, can I create a class , lets say 'Python' in c# and a member function 'execute_script' which doesn't use any api like iron python or doesn't create a process for executing the script, so that if call 'execute_scipt(mypythonprogram.py)' , my script gets executed. Sorry if this seems dumb. If this is possible, please do help me. Thanks in advance.
You can embed python into your application. View the python documentation section Extending and Embedding > Embedding Python in Another Application.
edit: this requires C++, but it should not be too difficult to build a C++ wrapper that you can use from C# (see this MSDN article).
Can you create a C# class that calls a Python script without using Iron Python and without using any external API? No. That is not possible. You have a few other choices:
Integrate the Python runtime into your program.
Smead already described one way to do this. It will work, and it does avoid creating another process, but it will be a lot of work to get it running, and it is still technically using an API. I do not recommend this for a single Python script where you don't need to pass data back and forth, but it's good to know that option exists if your other options don't pan out.
Use the Process module.
This is probably what I would do. Process has security concerns when a malicious user can cause you to execute bogus shell commands, or if the malicious user can replace the contents of the Python script. It is quite safe when you can lock down those two things.
The speed is unlikely to be a concern. It will literally only take a few minutes to set up a C# program with a process call, so if your mentor is concerned about speed, just write it and measure the speed to see if it's actually a problem.
Consider rewriting the script in C#
C# is a very expressive language with a very strong standard library, so assuming your script is not thousands of lines long, and does not use any obscure Python libraries, this might actually not be much work. If you really must not use Process, this would be the next solution I would consider.
is it possible to invoke function which is written in Java using WCF or any class application written in C# .net
Can it be possible by using webOrb..
i can't find enough information about Java to .Net remoting..
If you want to communicate between C# and Java you have a couple of options.
The cleanest: Build a service.
This assumes you have access to the source code of both your C# component and your Java component. In the case that you want to call a method within Java, you can build a service that allows a connection from your C# client, to your Java service, and the service then executes the desired functionality, and returns a value back to the C# client. Some easy ways to do this is by building a RESTful service or using Thrift. I recommend you choose a solution similar to this one.
The most complex: Corba
Corba is a standard defined to communicate amongst different computer languages. Most mature languages have support for it, but it is a bit unusual, and the use of it has declined in favor of building service. This also assumes access to both source codes.
You'd have to independently look for the information regarding how to use Corba on both Java and C#. I would really advice against this.
The dirtiest but quickest: Execute as process and parse output
I really do NOT recommend you to do it this way unless you really have no choice. This would entail executing a Java program from within C#. This is only a good choice when you have no other option, because all you have is an executable. If that were the case, you can use the Process class to execute the external program, sending it parameters, and then reading the output. See the example mentioned here:
How do I start a process from C#?
This has many downsides though, as you'll have to think of every exceptional cause, determine the output for those cases, and then determine how to parse that output. If the program has any level of complexity, before you know it, you'll end up with hard to maintain code.
Conclusion: Build a Service
That's probably your best bet. Build a service that exposes an API that the C# client can call on.
We are using JCOBridge package: it is able to create a bidirectional invocation of Java API from C# (.NET Core/6/Framework).
The templates available on Templates was our good starting point for the needs we had. We reach the goal in few lines of code.
UPDATE 2022: the JNet project on GitHub can be used as a starting point. Another project is KNet, hosted on GitHub and based on JNet, that is a gateway for Apache Kafka Java API.
I have a pet project that is an online game, the entire game engine is written in C# and I would like to know if there is anyway I can call the functions of this existing assembly (.dll) from a solution built using Node.JS, Socket.IO, Express etc?
The game engine itself is pretty complete; tested and robust. I am hoping there is some neat way of exposing its functionality without too much overhead.
UPDATE:
To answer my own question a little..
I have ended building my own web socket server (based on the most current web socket protocol document). It is written in C# and compiled using Mono so that it can be hosted on a Linux box running mono and therefore (with a few tweaks) I can use my existing game engine.
UPDATE 2
A project that does exactly what I was originally looking for now exists - http://tjanczuk.github.io/edge/#/
UPDATE 3
Edge.js supporting node's last versions and .net core with a new edge-js package.
Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x, 11.x Support for .NET
Core 1.0.1 - 2.x on Windows/Linux/macOS. Support for Mono runtime
4.8.x - 5.x.
Can be installed from https://www.npmjs.com/package/edge-js
Check out the edge.js project I started (http://tjanczuk.github.com/edge). It provides a mechanism for running .NET and node.js code in-process. Edge.js allows you to call .NET code from node.js and node.js code from .NET. It marshals data between .NET and node.js as well as reconciles the threading models between multi-threaded CLR and single threaded V8.
Using edge.js you can access islands of pre-existing .NET code from node.js, which seems to match your scenario.
I've been recently faced with the same challenge (requirement to call C# code from node.js javascript). I had 1000s of lines of complex C# code that I really didn't like to port to javascript.
I solved if as follows.
The relevant C# code is basically 1-2 classes in a DLL assembly
Defined a COM interface which is a subset of the C# class's interface and implemented that interface in the C# class. Thus, the DLL became an in-process COM server.
Implemented a node.js extension DLL that instantiates my C# COM class using standard Win32 COM API and routes method calls from node.js javascript to C# code using the COM interface.
This solves the problem if one only wants to make calls in one direction. I also had the requirement to make calls from C# to javascript. This is a lot harder. One has to:
Implement a COM object in the node.js extension DLL (ATL helps here)
Pass an interface reference of this COM object to C# code (COM Interop)
Route calls via the COM object to V8 objects in node.js
Maybe if I have some extra time, I might make an example project out of this.
If all you want to do is spin up a lightweight HTTP server while still programming with C# and .Net you should give Kayak a chance. It is a lightweight HTTP Server for C# and behaves kind of like node.js in that sense.
kayakhttp
Update:
If you are looking for a lightweight HTTP Server to handle web requests you have a couple alternatives today:
ServiceStack (recommended)
Microsoft WebAPI
NancyFx
To my knowledge all the above work on some version of Mono, so you can still host them across both Windows and Unix based systems.
.Net addons can be written, in short you write a regular native addon and add .Net calls via CLI/C++ calls to .Net dlls.
In practice you usually create a C# dll library which you then call from a CLI/C++ node addon project. There is a bit of delicacies such as making sure that the actual node add on definition file is compiled without CLR support so node can load it correctly.
You can check out: https://github.com/saary/node.net
for an example of how this can be achieved.
The following answer is out of date, but still helpful for understanding of Node.js from first release
Node.js is now also available natively for Windows at nodejs.org. No cygwin requirement or otherwise.
First of all, at the moment there's no native Windows port of Node.js, there's only a cygwin version (but I suspect you already knew that).
There was a node module floating around somewhere at the GitHubs that provided wrappers for calling into native libraries, but iirc, that only worked with .so libs.
Therefore, if you want to use a C# DLL, you will first have to write a native Node.js extension as the interface:
https://www.cloudkick.com/blog/2010/aug/23/writing-nodejs-native-extensions/
From that extension you have to load the DLL and wrap the calls from Node.js to the C# code, that means you have to write some low level C/C++ code and convert C# values to V8 stuff.
I only have experience with C++ and V8, it's a bit hard to get started since the code examples are a bit sparse, also wrapping C++ classes is not that trivial. But I did wrote small JS game engine kind of thing, that uses a C++ OpenGL backend, it's unfinished (and there are hardly any comments) but it might give you some ideas.
Note: There are some projects in the wild that provide somewhat automatic generation of wrappers to V8, but those are C++ only.
So to conclude, I think it will be quite adventurous getting the C# wrappers to work, but it should be possible.
Edge.js supporting node's last versions and .net core with a new edge-js package.
Support for Node.Js 6.x, 7.x, 8.x, 9.x, 10.x, 11.x Support for .NET
Core 1.0.1 - 2.x on Windows/Linux/macOS. Support for Mono runtime
4.8.x - 5.x.
Can be installed (npm i edge-js) from https://www.npmjs.com/package/edge-js
You might have some luck with this project, which is a port of Node.js to .NET. I haven't used it myself, but with a native .NET implementation you theoretically should be able to do what you need to.
You might also want to go the other direction and try to port (aka: recompile unless you're hooked deep into Windows) your C# game engine to Mono and see if you can then build wrappers off of that.
I know it's an old question, but wanted to throw in a current answer. With IIS 7.5 and .Net 4.x Websockets are supported, though use of the SignalR library will likely be the path of least resistance. It's similar to the socket.io library for NodeJS.
As to accessing .Net code via NodeJS, your best options are Edge.js, building a mixed native assembly with C/C++, exposing your .Net code either via a command line application (best to use pipes for input/output) or via a service (TCP or other).
I find Edge.js to be very limited, and not offer much over a piped console interface.. and feel that a service may be best for a more complex interface. At which point you may be best doing the rest of the project in .Net, unless you have an investment in NodeJS that supersedes said difficulties.
I have a c# application that defines a membership provider used in a Asp.Net MVC application.
And i have an apache httpd server that does authentication with mod_wsgi.
The objective is to share the membership provider between the two, so that the authentication information be the same.
How can i achieve this behaviour ?
Trivially.
Apache serves static content.
Certain URI's will be routed to mod_wsgi to Python.
Python will then execute (via subprocess) a C# program, providing command-line arguments, and reading the standard output response from the C# program.
Python does whatever else is required to serve the web pages.
This presumes your C# application runs at the command line, reads command-line parameters and writes its result to standard output. This is an easy thing to build. It may not be the way it works today, but any program that runs from the command line is trivial to integrate.
Your C# application, BTW, can also be rewritten into Python. It isn't magic. It's just code. Read the code, understand the code, and translate the code. You'll be a lot happier replacing the C# with something simpler.
Several ways:
COM interface (if Windows OS), although this would be a bit slow (make a COM-compatible library, register it with regasm, use it).
Using Gearman (not sure if faster than COM and whether it has Python and C# support, the investigation is up to you) http://gearman.org/
Using the method described by S.Lott
Using SOAP (slow, big)
We've written a Java program which we are looking to use and interact with from C#. What are our options? Optimally it would be possible to compile the Java application as a library (.DLL) that we could reference from C# perhaps using P/Invoke. This, however, doesn't appear to be an option according to the first few searches online.
We opt to be able to use ASP.NET to built a search engine powered by the Java code, so if this opens up for any other options please let us know.
Sorry, you cannot call java code / classes Directly from C# code.
One way of doing this is to wrap up your java classes in a java Web Service and call classes indirectly through that web service interface in your C# code.
Another way is using
javareg.exe which exposes java classes as COM. You can find it at following location:
C:\Program Files\Microsoft VisualStudio\VIntDev98\bin\javareg.exe
Following posts might help as well
Calling Java Classes Directly from
.NET (uses runtime bridge)
Calling Java from Microsoft.NET
The simplest approach would probably be to publish the functionality of your java library as web services and add a web-reference from your asp.net application.
Java isn't meant to be embedded in another program, so you need a bridge. The most simple solution is to use a socket: Create a Java process which listens for commands on a socket. In the C#, send the commands to the socket and read the answers.
The main problem here is serialization but if you use XML, it's not such a big pain anymore. Try the built-in XML serialization (see this article) or custom frameworks like XStream or Simple.
It is certainly possible to wrap Java in a .dll, and has been a part of the core Java platform for over 10 years. JNI (Java Native Interface) has an interface for embedding a JVM in your code, meaning you can run Java classes using C-style linking. Note that this will require that you write a simple C wrapper, there are samples within:
http://java.sun.com/docs/books/jni/html/invoke.html#11202
As some of these other posts suggest, sometimes it's desirable to be less tightly coupled, so you may want to consider using another design. One option would be a simple database, where the Java application regularly polls for requests from the C# code. If you want tighter coupling, for things like call-backs, you can look at distributed interfaces.