进程输出流的 WCF 基本 HTTP绑定流无法按预期工作
本文关键字:工作 绑定 输出流 WCF 基本 HTTP 进程 | 更新日期: 2023-09-27 18:36:14
我在 WCF 流式处理方面遇到了问题。我的解决方案中有两个组件:
- Windows 服务中承载的 WCF 服务
- 使用服务的客户端应用程序(目前为控制台应用)
WCF 服务只有一个方法:RunProcess。此方法不带任何参数,并且正在启动一个进程。它返回一个 Stream(这是一个 WCF 流式处理方法),对应于进程标准 ouptut 流 (StandardOutput.BaseStream)。
WCF 服务公开具有基本 HttpBinding 的单个终结点(使用 transferMode="streamed")。
客户端代码非常简单,它调用 RunProcess 方法并将 Stream 结果传递到在其上调用 ReadLine() 的 StreamReader 中,直到流结束(返回 null) 的 ReadLine() )。每个 ReadLine() 结果都会发送到控制台 (Console.WriteLine())
所以这很简单...但是,当我启动客户端时,控制台不会按预期立即刷新。它正在等待启动的进程结束的某种原因,因为长时间控制台中没有显示任何内容,并且一旦该过程完成,它将立即显示所有输出。
我还有一个控制台项目,不依赖于 WCF 服务,我直接启动进程,获取标准输出流,并使用完全相同的算法将流写入控制台,并且不存在问题,输出流在进程启动后直接显示,实时。
我不明白这种行为。任何帮助将不胜感激!
谢谢。
编辑:一些代码/配置确实可能会有所帮助。
--- 绑定的配置
<basicHttpBinding>
<binding name="BasicHttpBindingStreamed" maxReceivedMessageSize="67108864" transferMode="Streamed"/>
</basicHttpBinding>
--- WCF 方法
public Stream RunProcess()
{
Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "XXXXXX";
p.Start();
return p.StandardOutput.BaseStream;
}
--- 客户康萨默
Stream cmdOutputStream = serviceClient.RunProcess();
string currentLine = null;
using (TextReader reader = new StreamReader(cmdOutputStream))
{
currentLine = reader.ReadLine();
while (currentLine != null)
{
Console.WriteLine(currentLine);
currentLine = reader.ReadLine();
}
}
已解决...愚蠢的问题,我在WCF服务器端配置上指定了transferMode="Streamed",但忘记了客户端。