views:

880

answers:

5

Hi, I´m trying to run an old .NET application from an ASP.NET website. After reading the web and Stackoverflow (for similar problem) I come to the following code. The Problem is that I get always an error code (I am using administrator account just to testing purposes). If I run the exe manually it works ok.

private void Execute(string sPath)
{
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.UserName = "administrador";
    string pass = ".............";

    System.Security.SecureString secret = new System.Security.SecureString();
    foreach (char c in pass) secret.AppendChar(c);

    proc.StartInfo.Password = secret;
    proc.StartInfo.WorkingDirectory = ConfigurationManager.AppSettings["WORKINGDIRECTORY"].ToString();
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.FileName = sPath;
    proc.Start();
    proc.WaitForExit();
    string result = proc.StandardOutput.ReadToEnd();
    Response.Write(result + " - "  + proc.ExitCode);
    proc.Close();
}

}

The exitcode I get is: -1066598274 Result variable is empty. No exception is thrown I am using Windows 2008 with IIS 7.0

Thanks in advance,

Ezequiel

A: 

You may need to set the proc.StartInfo.LoadUserProfile property to true so the administrator's user profile stuff is loaded into the registry (AFAIK this does not happen by default).

Also, it might be educational to run a 'hello world' program to see if the problem is with actaully creating the process or if the process itself is having problems running in the context it's given.

Finally, as a step in trying to narrow down where the problem might be, you might want to run the ASP.NET process itself with admin or system credentials to see if something in the permissions of the account the ASP.NET instance is running under is part of the problem (but please do this only for troubleshooting).

Michael Burr
Thanks for your answer, I added the loadUserProfile. I´ve alse changed the .exe for cmd.exe and I still get the same exit code. Any other idea?
Ezequiel
+1  A: 

Don't do this. This is just plain dirty and should not be done from ASP.NET

  1. Write a windows service
  2. Store the request in a queue
  3. The service should poll the queue and process. If needed run the exe. It is suggested that the service stays in a different server.

Don't do this. This is very bad and not scalable and bad for the web server

Don't

Don't

Don't

Shafqat Ahmed
Agree with this.
Sbm007
+1  A: 

If you use

proc.StartInfo.RedirectStandardOutput = true;

then you have to read the stream as the process executes, instead of before the call to

proc.WaitForExit();

Same goes for the standard error stream. See the MSDN docs for more detail.

omellet
+1  A: 

You need to reorder the output reading at the end.

It expects you to read before the waitforexit() call, so you should have:

proc.Start();
string result = proc.StandardOutput.ReadToEnd();
Response.Write(result + " - "  + proc.ExitCode);
proc.WaitForExit();
proc.Close();

Fiona Holder
+1  A: 

If the application you're trying to run is really a .NET application as you say, you may not need to run it in a separate process at all. Instead, you can take advantage of the fact that .NET executables are also assemblies. I don't think Visual Studio will let you reference assemblies that end in .exe, but the command-line compiler will.

I would try using the command-line compiler to create a wrapper assembly that simply references the executable assembly, and directly calls its Main() method, passing in a string array of any command-line parameters you would normally specify. The exit code, if any, will be an integer return value from the Main method. Then you can simply call your wrapper assembly from your ASP.NET app.

Depending on what the executable does, and how much it interacts with the console, this approach may not work at all. But if it does work for your case, it should perform much better than spinning up a separate process.

Joel Mueller