Process.Start from a web service and StandardOutput

S

Steve B.

Hi,

I'm written a Web Service that encapsulate the call to a command line
application.
The command line application is run using Process.Start, and I'd like to get
both standard ouput and standard error to create a log.

After some searches, I finally wrote this part of code :

public class MyClass {

private StreamWriter sw; // A Streamwriter where we can write the log
private Process p;

public void DoSomething(
string arg,
Stream outputStream
)
{
try
{
this.sw = new StreamWriter(outputStream); // An output stream will contains
the log

p = new Process();
p.StartInfo.FileName = "c:\\myapp.exe";
p.StartInfo.Arguments = string.Format(
"myarg=\"{0}\"",
arg
);
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
p.Start();
p.WaitForExit();
}
catch (Exception exc)
{
sw.WriteLine(exc.ToString());
throw exc;
}
}
void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}

private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
sw.Write(e.Data);
}

}

Is it the right way ?

Thanks,
Steve
 
S

sirfunusa

MyApp is going to run on the server, you know that, right? Spawning
apps like this is never a good idea. What does the app do?
 
S

Steve B.

The app actually runs on the server.
It is command line that can sign some assemblies using SIGNTOOL.EXE.

The Web Service is used to maintain a device application auto update feature
based on CAB files that must be signed with a certificate.

I send the raw dll and exe files to the webservice, and the WS first create
the cabfile using makecab (well working), and then sign the output cab with
SIGNTOOL.EXE (not well working).

Since it does not work, I want to get the process output to return to the
user in order to know why it did not worked.

I agree Process.Start and derivated in a WS is not a nice solution, but
building the full mecanims of creating and signing cab files is surely quite
complex. That's why we decided to use the existing tools, even if they are
command line tools.

We'll stay on this solution for now because of a release soon, but if you
have suggestion on other solution, we listen :)

Thanks,
Steve
 
S

Steve B.

In orderto test, I've an incredibly complex command line app :

public static void Main()
{
Console.WriteLine("Bonjour");
for (int i = 0; i < 100; i++)
{
Console.Write(i.ToString("00") + " ");
Thread.Sleep(25);
}
Console.WriteLine("\nFini");
}

Running this command line within a WS does not produce any output...

Thanks,
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top