Pipe STDOUT/STDIN with two process and a web output stream in C#
I have two processes that I'd like to pipe together with C#, and then send the output of the second one directly to an HTTP output stream.
In a command line, I can do this and it works perfectly (where "-" represents stdin/stdout):
proc1 "file_to_input" - | proc2 - -
My problem is c开发者_StackOverflow社区onnecting these two processes in C# and being able to take proc2's STDOUT and port it directly to the web, without buffering the entire thing first. The inputs and outputs will be a binary datatype, so I'll need to convert it from the default StreamReader/Writer.
Is this possible, and what is the best way to go about it? Thanks!
Try CliWrap:
var cmd1 = Cli.Wrap("proc1").WithArguments(a => a.Add("file_to_input").Add("-"));
var cmd2 = Cli.Wrap("proc2").WithArguments(a => a.Add("-").Add("-"));
await (cmd1 | cmd2).ExecuteAsync();
If you want to then redirect the output to some other stream (e.g. HTTP stream), then:
var output = new MemoryStream();
var cmd1 = ...;
var cmd2 = ...;
await (cmd1 | cmd2 | output).ExecuteAsync();
Unless I'm misunderstanding you, this is the normal "read from one stream, write to another" situation, where you declare a buffer of some size and just keep reading from the input and writing to the output until there's nothing left to read from the input.
In .NET 4.0 they added a CopyTo method on to Stream to make this simpler, but you can just do the same code yourself.
Stream source = ...;
Stream destination = ...;
byte[] buffer = new byte[4096];
int read;
while ((read = source.Read(buffer, 0, buffer.Length)) != 0) {
destination.Write(buffer, 0, read);
}
In your case, it sounds like you can get source via Console.OpenStandardInput - for the destination I'm not 100% sure what you mean by "port it directly to the web", but since we're a console app and not an asp.net page, I'm assuming you're sending it to some destination page. If you're using WebClient, you can use its OpenWrite method. If it were an asp.net page reading this from a separate process, it could write it to Response.OutputStream instead.
Related links:
http://msdn.microsoft.com/en-us/library/system.console.openstandardinput.aspx
http://msdn.microsoft.com/en-us/library/system.net.webclient.openwrite.aspx
http://blogs.msdn.com/bclteam/archive/2009/10/21/what-s-new-in-the-bcl-in-net-4-beta-2-justin-van-patten.aspx
http://msdn.microsoft.com/en-us/library/system.io.stream.copyto(VS.100).aspx
精彩评论