我怎样才能从流WCF没有缓冲的回应? [英] How can I stream a response from WCF without buffering?

查看:237
本文介绍了我怎样才能从流WCF没有缓冲的回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个宁静(的WebHttpBinding)自承载WCF服务。大多数方法正在返回的对象XML或JSON版本给客户端。

I have a restful (webHttpBinding) self-hosted WCF service. Most methods are returning xml or json version of objects to the client.

我有几个触发长时间运行的方法GET方法,我想流日志效应初探,以使用户知道是怎么回事的浏览器(或应用程序)。这将是简单的完成与 HttpContext.Current.Response.OutputStream.Write 。不幸的是, HttpContext.Current 总是空的自承载WCF服务,即使我包括 aspNetCompatibilityEnabled 配置( IIS是不是一种选择不幸)。

I have a couple of GET methods that trigger long running methods and I'd like to stream the log reponse to the browser (or application) so that the user knows what's going on. This would be simple to accomplish with HttpContext.Current.Response.OutputStream.Write. Unfortunately, HttpContext.Current is always null in a self-hosted WCF service, even if I include the aspNetCompatibilityEnabled config (IIS is not an option unfortunately).

我已经试过 AnonymousPipeServerStream WCF和流媒体请求和响应

随着第一个设置:

OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
context.ContentType = "text/plain";

,以便响应进入浏览器不会下载流成一个文件来保存。

so that the response comes into the browser it doesn't download the stream into a file to save.

在Chrome浏览器不会在所有的工作 - 它缓冲,直到结束。在IE浏览器或wget的出现在一个时间缓冲周边的4K(或其它)。这是没有良好的记录的,除非我吐了不必要的日志信息的负载力输出,用户并不知道发生了什么事情。我只能假设这是因为它最终可能分块响应和块是4K(而不是仅仅写的OutputStream)。

In Chrome it doesn't work at all - it buffers until the end. In IE or wget it appears to buffer for around 4k (or something) at a time. This is no good for logging as unless I spit out loads of unnecessary log messages to force output, the user doesn't really know what's going on. I can only assume that this is because the response is actually a chunked response and the chunks are 4k (rather than just writing to the outputstream).

让Chrome浏览器输出的修补程序显然是在发送分块的响应之前写一些垃圾内容:<一href="http://stackoverflow.com/questions/13557900/chunked-transfer-encoding-browser-behavior">Chunked传输编码 - 浏览器的行为的,但是,我不认为这是可能的WCF

The fix to get chrome to output is apparently to write some garbage to the content before sending the chunked response: Chunked transfer encoding - browser behavior, however, I don't think this is possible with WCF.

所以,可能的解决方案,我在寻找:

So, possible solutions I'm looking for:

  • 系统的方式来写的OutputStream在WCF在自托管的服务(不包括IIS)。 或
  • 办法控制的块大小在流响应(安培;一种方法,先写一些内容,以便Chrome浏览器将呈现块)。
  • A way to write to the outputstream in WCF in a self hosted service (without IIS). or
  • A way to control the chunk sizes in a stream response (& a way to write some content first so that Chrome will render the chunks).

另一种选择,我想,是沟WCF有利于更多的东西REST友好(我开始觉得WCF是不是正确的选择)。不过,在写了这么多的WCF现在,这似乎是一个乏味的任务。除非有什么东西我可以切换到这将是一件容易的迁移(例如,如果我可以只用不同的属性重用相同的服务类,也许)。南希可能?

Another option, I suppose, is to ditch WCF in favour of something more REST friendly (I'm beginning to think WCF wasn't the right choice). However, having written so much in WCF now, this seems like a tedious task. Unless there is something I can switch to that would be an easy migration (e.g. if I could reuse the same service classes, perhaps with just different attributes). Nancy maybe?

推荐答案

我已经做了一些像你问这里 - 自我托管了。我写了一个WCF(basicHttpBinding的)服务,既做流和数据来消费我的服务进行数据同步客户端设备的缓冲。流媒体是很难的,因为你可能已经想通了,我不认为有什么办法写入流。

I've done something like what you're asking about here - self hosted, too. I wrote a WCF (BasicHttpBinding) service that did both streaming and buffering of data to client devices consuming my service for data synchronization. Streaming is hard, as you've probably figured out, and I don't think there's any way to "write into the stream".

在一个基本的意义上说,流过一个WCF服务的工作是File.IO的工作方式相同,如code图所示。

In a basic sense, Streaming over a WCF service works the same way that File.IO works, as seen in the code below

 FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
 BinaryReader br = new BinaryReader(fs);

如果有问题的文件是1 GB,你的文件流将开始之前,其读取到该文件的末尾返回字节。流过WCF的工作方式相同(事实上,它实现的FileStream,以我的经验),这就是为什么它是很好的数据大块。它读取...发送;它读取...发送。所以,我不知道你如何输出注入一些信息转换成流,你的屏幕。

If the file in question is 1 GB, your filestream will begin returning bytes before its read to the end of the file. Streaming over WCF works the same way (in fact, it implements FileStream, in my experience), which is why it's good for huge chunks of data. It reads ... it sends; it reads ... it sends. So I'm not sure how you'd inject some information into that stream for output to your screen.

说了这么多,我们的同步UI显示字节下来的数量,再加上完成的百分比,以保持用户关闭机器或取消。为此,我们有一个单独的线程读取的下载文件,每10秒,并计算整体的百分比(完整的大小是送回作为响应的参数),然后将结果写入输出到UI结果窗口的大小。因此,解决方案实际上是pretty的简单,在我们的例子。

Having said that, our Synch UI displays the count of bytes coming down, plus the percentage complete, to keep the users from turning off the machine or canceling. We do this by having a separate thread reads the size of the downloading file every 10 seconds and calculating the percentage of the whole (the complete size is send back as a parameter in the response), then writing the results out to the UI results window. So the solution is actually pretty simple, in our case.

这篇关于我怎样才能从流WCF没有缓冲的回应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆