By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,262 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,262 IT Pros & Developers. It's quick & easy.

C#/IIS : Command line ouput can't be read when running under IIS

P: 6
Hi,

I have some code that I want to use to run a command line utility and I want to be able to run it from an aspx page running under IIS. The command line utility is a local utility running on the same box as my IIS server, and the code works in Visual Studio, just not under IIS. Any suggestions?

The logs are totally silent on the issue. On line 36 (below), the log says:
"I read this from our process:" . . . .just blank after that.

Grrrr.....


Expand|Select|Wrap|Line Numbers
  1.     public class CommandLineUtils : ICommandLineUtils
  2.     {
  3.         private static readonly ILog log = LogManager.GetLogger(typeof(CommandLineUtils));
  4.  
  5.         private StringBuilder processStdOutput = null;
  6.  
  7.         /// 
  8.         /// <summary>
  9.         /// Executes an Interwoven command line utility
  10.         /// </summary>
  11.         /// <param name="clt">The utility to execute</param>
  12.         /// <param name="args">Any arguments to pass on the command line</param>
  13.         /// <returns>The string of output, as produced by running the command</returns>
  14.         /// 
  15.         public string ExecIwovCLT(string clt, string args)
  16.         {
  17.             log.Debug("Executing command: '" + clt + " " + args + "'");
  18.             processStdOutput = new StringBuilder("");
  19.  
  20.  
  21.             Process proc = new Process();
  22.             proc.StartInfo.FileName = clt;
  23.             proc.StartInfo.Arguments = args;
  24.             proc.StartInfo.UseShellExecute = false;
  25.             proc.StartInfo.RedirectStandardOutput = true;
  26.             proc.StartInfo.RedirectStandardError = true;
  27.  
  28.             proc.OutputDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
  29.             proc.ErrorDataReceived += new DataReceivedEventHandler(proc_OutputDataReceived);
  30.  
  31.             try
  32.             {
  33.                 proc.Start();
  34.                 proc.BeginOutputReadLine();
  35.                 proc.WaitForExit();
  36.                 log.Debug("I read this from our process: \n" + processStdOutput.ToString());
  37.             }
  38.             catch (Win32Exception ex)
  39.             {
  40.                 log.Error("Could not execute the '" + clt + " " + args + "' command");
  41.                 log.Error(ex.ToString());
  42.             }
  43.             finally
  44.             {
  45.                 proc.Close();
  46.             }
  47.  
  48.             return processStdOutput.ToString();
  49.         }
  50.  
  51.  
  52.         /// 
  53.         /// <summary>
  54.         /// Event handler for process execution method (above)
  55.         /// </summary>
  56.         /// <param name="sendingProcess">process doing the outputting</param>
  57.         /// <param name="outLine">output line from the process</param>
  58.         /// 
  59.         private void proc_OutputDataReceived(object sendingProcess,
  60.                                              DataReceivedEventArgs outLine)
  61.         {
  62.             if (!String.IsNullOrEmpty(outLine.Data))
  63.             {                
  64.                 processStdOutput.Append(outLine.Data + Environment.NewLine);
  65.             }
  66.         }
  67.     }
  68. }
  69.  
Jan 3 '08 #1
Share this Question
Share on Google+
6 Replies


Plater
Expert 5K+
P: 7,872
I don't see anywhere where you actually read the data from the process (the in/out/error streams)
I see you have the eventhandlers, but they don't seem to ever look at the streams?
Jan 4 '08 #2

P: 6
I don't see anywhere where you actually read the data from the process (the in/out/error streams)
I see you have the eventhandlers, but they don't seem to ever look at the streams?
The line:
Expand|Select|Wrap|Line Numbers
  1. processStdOutput.Append(outLine.Data + Environment.NewLine);
is appending Stdout and Stderr lines to a StringBuffer class member variable.

I know that part works (i.e. that I can actually read stuff from a command line handle) because when I run this in my IDE and I try executing the DOS command "date" (for example), I can read the process' output and I echo the correct time.

When I move this code in to IIS, however, I just read nothing. . .an empty line.

BTW, I found out that the exit code of the Process is "1", but that's all the info I can get...it isn't throwing an exception or telling me why it's erroring out.
Jan 4 '08 #3

Plater
Expert 5K+
P: 7,872
Yeah, sorry, I went crosseyed and realized I was reading the eventhandlers wrong (oops).

Even if it exited with a zero, shouldn't it dump data into the error stream?

Maybe it never actually executed the file you are trying to run. Like if you tried to run a .vbs file but didn't have vbscript support on the computer. It wouldn't return any data to the in/out/error streams because it couldn't do anything, that would return a non-zero value for the return code.
Have you tried manually running the process on the IIS server machine?
Jan 4 '08 #4

P: 6
Yeah, sorry, I went crosseyed and realized I was reading the eventhandlers wrong (oops).

Even if it exited with a zero, shouldn't it dump data into the error stream?

Maybe it never actually executed the file you are trying to run. Like if you tried to run a .vbs file but didn't have vbscript support on the computer. It wouldn't return any data to the in/out/error streams because it couldn't do anything, that would return a non-zero value for the return code.
Have you tried manually running the process on the IIS server machine?
Yep, in my logs, I echo exactly what I'm trying to run:
Expand|Select|Wrap|Line Numbers
  1. Executing command: 'C:\Interwoven\TeamSite\bin\iwgetwfobj.exe 123'
  2. I read this from our process: 
  3.  
  4. 2008-01-04 11:08:18,583 DEBUG The process exit code is: '1'
  5.  
So if I copy and paste the command onto a DOS prompt on that server, I get the expected output (about 20 lines of XML).

Weird, huh?

To make things more weird, I even tried running some other command just before the iwgetwjobj command....and that works!

Just before I try running the iwgetwjobj.exe, I run "c:\\cygwin\\bin\\ps.exe" and it returns a list of processes currently running on that server, and that works when I run it on the command line, from within my IDE, and (crucially) while running under IIS.

So I'm really kind of stuck as to why one exe will work and the other won't.
Both exe's have the same permissions (Everyone has Full Control).
Jan 4 '08 #5

Plater
Expert 5K+
P: 7,872
Did you run the ps.exe process in the same manor? capturing it's output(s) and setting shellexecute to false?
Jan 4 '08 #6

P: 6
Did you run the ps.exe process in the same manor? capturing it's output(s) and setting shellexecute to false?
Yep; exactly the same method used in both cases.
Expand|Select|Wrap|Line Numbers
  1.         public string GetReviewTaskOwner(int taskid)
  2.         {
  3.             //
  4.             // FIXME remove this ps.exe debugging call:
  5.             //
  6.             string testcommand = "C:\\cygwin\\bin\\ps.exe";
  7.             string testoutput = clUtils.ExecIwovCLT(testcommand, "");
  8.             log.Debug("The testoutput of running " + testcommand + " is \n'" + testoutput + "'\n");
  9.  
  10.             //
  11.             //  This is where the iwgetwfobj call is made
  12.             //
  13.             string fullPathClt = Path.Combine(iwbin, iwgetwfobj);            
  14.             string args = "" + taskid;
  15.             string commandLineOutput = clUtils.ExecIwovCLT(fullPathClt, args);
  16.  
  17.             string owner = getOwnerFromWfObjXML(commandLineOutput);
  18.  
  19.             return owner;
  20.         }
Jan 4 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.