What to use to process JSON responses on a command line? - c#

I have a nightly executable that will run on a windows or linux server that will be downloading information from various web sources and one of those sources contains a JSON response. This executable will download the information and connect to a SQL server database to update the appropriate records.
I come from a C#, windows programming background so my natural inclination is to use the JSON.net libraries or create custom code to parse the JSON text using C#. But I'd really rather use the appropriate scripting language to take advantage of the eval() statement to process the JSON.
Does anyone have a suggestion for which scripting language and development environment that would be best suited for this kind of server process? It doesn't matter to me if it runs on the linux or windows server, I just want to use the best tool available.

The eval() technique only works within JavaScript, as JSON is valid JavaScript syntax. You might be able to use something like Rhino (Java), but otherwise every language you choose will require using a JSON library. Since you are most familiar with C# and .NET, it seems like the most logical choice if the target machine already has it available.

JSONsharp (.Net 2.0+) or JSON.net (.Net 3.5) are your friends here, as are others on JSON.org.
But beware the eval() statement in JavaScript - it executes the JSON, so if someone has put javascript in there, you may find yourself running code. Instead you would want to use something like JSON.parse.

Any scripting environment should be fine, and nearly all have decent JSON parsers built in.
Here's a Python example (requires Python 2.6 and up for the built-in json module, otherwise download simplejson):
import urllib2, json
response = urllib2.urlopen("http://example.net/url/for/json").read()
data = json.loads(response) # returns a native Python type, e.g. a dictionary
# your code here
process_and_update_db(data)
If you want to use .NET (may be easier to access your DB, for example), and want a good scripting language like Python, try IronPython. You can use download and use simplejson instead of json.

Related

Running Python Code in .NET Environment without Installing Python

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.

Support scripting in .Net

Is there a way to use C# as a scripting language? I want users to be able to write a method body and work with objects that are passed from the main program to the script method.
And maybe is there something more universal out there? So I can support more than C#?
Check out scriptcs. Scott Hanselman has a great blog post about it here. It allows you to do scripting with C#, either by running arbitrary scripts as *.csx files, written with with your favorite text editor or by using the REPL. It also has integration with NuGet to allow you to easily pull down dependencies.
The ScriptCs.Core library can be used to host script execution in your own application, like OctopusDeploy has done.
The scripting engine is pluggable. As of now, it uses Roslyn as the C# scripting engine, but there's also work being done to use mono as an alternative. There's nothing stopping you from implementing f.ex. F# support.
To make bootstrapping of popular frameworks even easier, there's script packs that reduces the amount of code you have to write to get something running quickly.
To get you started, check out the samples repository.

Call Java function using .Net(C#)

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.

Run C# File on the internet like a CGI File

I have a Visual C# project on my computer and I would like my users to be able to interact with it through a web browser. I've done my research, and I understand that I will probably use CGI to do this.
However, while I have been able do this with .pl and .cpp files, i can't figure out how to to it with C sharp (.cs) files. Can anyone explain how I would do this?
EDIT: If there is an alternate solution wherein the webpage communicates my C# program as a back-end program, that works, too.
UPDATE: After cancelling with my hosting provider and setting up a home-based web server (windows), I finally got the C# file to run as CGI. Thanks to everyone for your help!
You've got to give us some context ... why wouldn't asp.net be an option? If you simply don't want to run in the context of IIS, you could simply write a windows service (and expose WCF services, or even raw sockets if that's what you need).
edit: in response to the recent comment about the server being UNIX, you can use MONO to run .net code on that server: http://www.mono-project.com/Main_Page
You can either use something like mod_mono or if you'd prefer something different, you can look at manos de mono
First, I assume you have the Mono project compiler and runtime environment installed on your system.
CGI takes place entirely via standard input and standard output. (This is one reason why it is so easy to write CGI scripts in Perl, Python, Ruby, etc. You just start reading standard input as usual, parse the variables, and write whatever output you want the client to see.) Don't forget that you're responsible for the entire header, including Mime type.
If you need something that performs faster than CGI's constant fork(2)+execve(2) re-starting your CLR over and over again, you can implement the FastCGI protocol (at least, I didn't see a C# implementation on the Wikipedia page) yourself using sockets and start your CLR once only, so you have some reasonable performance. (I seem to remember Nat being passionate about the Mono process start time being "fast enough" to use it for interactive commands, so perhaps a lightly-loaded server won't have any trouble with plain old CGI interface.)

using c# inside an apache python script

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)

Categories