3

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.

slavoo
  • 5,798
  • 64
  • 37
  • 39
Primoz
  • 4,079
  • 17
  • 56
  • 67
  • 5
    Permissions, as you mentioned is most likely the issue. Can you impersonate a user with higher rights? – George Johnston Jun 30 '11 at 19:41
  • Based only on the error message, have you tried doing `WaitForExit` BEFORE doing `ReadToEnd`? Also, remove the timeout param, as it is right now if it takes >1 second you're going to have an exception. If that's what you want, catch and handle the exception. – Brook Jun 30 '11 at 19:44
  • @George How can I impersonate user ? Some settings in web.config ? – Primoz Jun 30 '11 at 19:53
  • @Primoz Yes, http://msdn.microsoft.com/en-us/library/aa292118(v=vs.71).aspx – George Johnston Jun 30 '11 at 19:54
  • also note that this cause with multiple calls would quickly starve the thread pool. if this is a 'once every now and then' type call - prob ok, but otherwise caution - I'd try an async method. – Adam Tuliper Jun 30 '11 at 20:04
  • I tried this, but it doesn't help. – Primoz Jun 30 '11 at 20:27

3 Answers3

2

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.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • @Primoz - Did you try with or without the statement before that line? I meant it to be tried without. – Justin Niessner Jun 30 '11 at 19:48
  • I tried like this: using (Process proc = Process.Start(pInfo)) { proc.WaitForExit(); exitCode = proc.ExitCode; } – Primoz Jun 30 '11 at 19:50
  • @Primoz - I just updated my answer. The process may also be writing something to StandardError and then hanging (rather than completing). You should check that as well. – Justin Niessner Jun 30 '11 at 19:53
  • Like this ? exitMsg = proc.StandardError.ReadToEnd(); proc.WaitForExit(); exitCode = proc.ExitCode; It still does nothing. – Primoz Jun 30 '11 at 19:57
  • @Primoz - Did that get you anything, or just cause it to hang again? – Justin Niessner Jun 30 '11 at 19:58
  • @Primoz let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1029/discussion-between-justin-niessner-and-primoz) – Justin Niessner Jun 30 '11 at 20:18
1

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.

Primoz
  • 4,079
  • 17
  • 56
  • 67
0

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.

Peter Bromberg
  • 1,498
  • 8
  • 11