Monday, May 04, 2009

Executing a Console App from ASP.Net

Well, I was fooling around with a .Net API that does google tanslations. I thought it might be a good idea to not call the translating API for bulk translations directly from the website, so I created a little console app so I can do that and do a thread.Sleep pause and control the SQL Connection opening and closing a little more robustly. Anyway, here is some sample code on how to open a console from ASP.Net in C#

 // path to the console app
string sPath = Server.MapPath("~/apps/GoogleTranslationConsole.exe");
// pass your path, and your arguments array into a ProcessStartInfo
ProcessStartInfo proc = new ProcessStartInfo(sPath,Request.Form["lagr"]);
// Incase you want to read messages back from the code
proc.RedirectStandardOutput = true;
// hide any command windows from showing up
proc.UseShellExecute = false;
proc.CreateNoWindow =
true;
// create the process that will control the console app
Process p = new Process();
p.StartInfo = proc;
p.Start();

// use console.writeline for data to return back.
// Also, be sure to either p.Dispose() of your process here or do
// do a System.Environment.Exit in your console app when work is done
//p.StandardOutput.ReadToEnd() for results.


Here is the original link that I got this info from, which is in VB.Net.

Opening a Console App in VB.Net

Note: I haven't deployed this code to production yet, I've only got it working on my local box. In a production environment you probably want to make sure that your console apps aren't accessible by the web, that your console app is secure enough so that it only does what is should do when it should do it, and that only the right people can execute it (thought for my purposes probably the IIS account will probably have permission to execute). Just something to watch for once you are to that point.

No comments: