Primoz
Primoz

Reputation: 4331

Why Process.Start doesn't work from asp.net web service?

Why this code runs perfectly on my development computer (win7 32bit) and on target server(2008r2 64bit) as console app. But when I try to run it as a web service on the target server it does nothing. No error, nothing.

If I remove

exitMsg = proc.StandardOutput.ReadToEnd();

then it fail with error:

System.InvalidOperationException: Process must exit before requested information can be determined.

   [WebMethod]
    public string GetRunningProcesses()
    {
        ProcessStartInfo pInfo = new ProcessStartInfo();
        pInfo.FileName = @"E:\bin\PsList.exe";        
        pInfo.WindowStyle = ProcessWindowStyle.Hidden;
        pInfo.CreateNoWindow = true;
        pInfo.UseShellExecute = false;
        pInfo.RedirectStandardOutput = true;

        string exitMsg = "";
        int exitCode = 1;

        using (Process proc = Process.Start(pInfo))
        {
            exitMsg = proc.StandardOutput.ReadToEnd();
            proc.WaitForExit(1000);
            exitCode = proc.ExitCode;
        }

        return exitMsg;
    }

I think there must be something about user under which code runs. As web service this code runs under asp.net user and this might couses the problems.

Please advice me how to fix this. Thank you very much.

RESOLVED


The problem was with EULA dialog, which poped up but it was invisble due to ProcessStartInfo settings. When I run PsList.exe via CMD under account which is also used for Application pool for this web service, I get prompted for an EULA agreement and after that everthing works fine.

The strange thing is that I have "pInfo.Arguments = "/accepteula";" in my real code. This should prevent my probem, but it didn't and I don't know why. If any of you knows why, please tell me.

Thank you very much for all the help. You are trully good peoples here.

Upvotes: 3

Views: 4820

Answers (3)

Primoz
Primoz

Reputation: 4331

The problem was with EULA dialog, which poped up but it was invisble due to ProcessStartInfo settings. When I run PsList.exe via CMD under account which is also used for Application pool for this web service, I get prompted for an EULA agreement and after that everthing works fine.

The strange thing is that I have "pInfo.Arguments = "/accepteula";" in my real code. This should prevent my probem, but it didn't and I don't know why. If any of you knows why, please tell me.

Thank you very much for all the help. You are trully good peoples here.

Upvotes: 1

Peter Bromberg
Peter Bromberg

Reputation: 1496

Try wrapping your business logic in a try / catch block that catches any exception and either writes it to the output or to a log file.

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245419

I think your only problem is with:

proc.WaitForExit(1000);

Which instructs the program to wait for a second for the process to finish. On your machine, the process finishes fine. On another machine, though, it may take longer. Try changing to:

proc.WaitForExit();

Which will wait indefinitely for the program to exit.

You may also want to redirect the output of the Process to see if the programming is hanging or waiting for something else from you (or, in this case, your code).

In addition, the process may be hitting an error and writing a message to StandardError rather than StandardOutput. Try setting pInfo.RedirectStandardError = true; and reading that as well to see if there's anything you're missing.

Upvotes: 2

Related Questions