Last time, I introduced this topic and gave an example of a custom stream implementation. This time, I'm going to make a few enhancements to this implementation and get into the topic of using it in a WCF Service.
Streaming with WCF
Part 2: Plugging a custom stream into a WCF service
So, I've made a couple modifications to the CustomStreamImplementation. I added a couple knobs to control when it stops streaming. Here's the complete code:
using System;
using System.IO;
using System.Threading;
namespace CustomStreamImplementation
{
public class ContinuousStream : Stream
{
// Used to control when Read will return 0.
public bool StopStreaming { get; set; }
public TimeSpan ReadThrottle { get; set; }
// Only set this if you don't want to manually control when
// the stream stops.
// Keep it low - less than 1 second. The server can send bytes very quickly (without a throttle), so
// sending a continuous stream will easily blow the MaxReceivedMessageSize buffer.
public TimeSpan StreamDuration { get; set; }
DateTime readStartedTime;
long totalBytesRead = 0;
public override bool CanRead
{
get { return !StopStreaming; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { throw new NotImplementedException(); }
}
Read more: James Osborne's Blog