【问题标题】:wcf basichttpbinding streaming for process output stream does not work as expected进程输出流的 wcf basichttpbinding 流无法按预期工作
【发布时间】:2016-08-19 17:46:44
【问题描述】:

我遇到了 WCF 流式传输的问题。 我的解决方案中有两个组件:

  • Windows 服务中托管的 WCF 服务
  • 使用服务的客户端应用程序(现在是控制台应用程序)

WCF 服务只有一种方法:RunProcess。此方法不带任何参数并且正在启动一个进程。它返回一个与流程标准输出流(StandardOutput.BaseStream)对应的Stream(这是一个WCF流方法)。

WCF 服务使用 basicHttpBinding(使用 transferMode="streamed")公开单个端点。

客户端代码非常简单,它调用 RunProcess 方法并将 Stream 结果传递给 StreamReader,在其上调用 ReadLine() 直到流结束(ReadLine() 返回 null)。每个 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();
    }
 }

【问题讨论】:

  • 也许发布一些代码和配置文件可能会有所帮助。

标签: c# wcf process streaming basichttpbinding


【解决方案1】:

解决了......愚蠢的问题,我在 WCF 服务器端配置上指定了 transferMode="Streamed" 但忘记了客户端。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-27
    相关资源
    最近更新 更多