I have a restful (webHttpBinding) self-hosted WCF service. Most methods are returning xml or json version of objects to the client.
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).
I have tried AnonymousPipeServerStream
:
WCF and streaming requests and responses
along with first setting:
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.
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).
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:
- 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).
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?