Streaming at LOW bitrate using RESTful WCF service
I want to continuously stream data at a very low bitrate : a few bytes per second.
My WCF REST service works fine when I stream a large amount of data. But when the bitrate is low, it seems that the stream is buffered until there is enough data to pass to the transport layer.
As a result, I receive 16Ko of data every 16 sec instead of 1Ko every sec.
How can I implement low bitrate streaming in WCF REST?
My WCF service:
void StartMyWCFService()
{
host = new WebServiceHost(typeof(IServ), n开发者_如何学运维ew Uri("http://localhost:4530/");
var bnd = new WebHttpBinding();
bnd.TransferMode = TransferMode.Streamed;
host.AddServiceEndpoint(typeof(IServ), bnd, "");
host.Open();
}
[ServiceContract]
public interface IServ
{
[OperationContract, WebGet(UriTemplate = "/")]
Stream MyStream();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class Serv : IServ
{
public Stream MyStream()
{
var resp = WebOperationContext.Current.OutgoingResponse;
resp.StatusCode = System.Net.HttpStatusCode.OK;
return _new MyStream();
}
}
class MyStream : Stream
{
public override int Read(byte[] buffer, int offset, int count)
{
// returns a few bytes every second
}
[...]
}
Edit: I noticed a 16Ko hardcoded buffer size deep inside .net framework (v4) at:
System.ServiceModel.dll#System.ServiceModel.Channels.HttpOutput.WriteStreamedMessage()
Thanks
You may be able to work-around the buffering by returning an IEnumerable of byte arrays instead of a stream, and sending a byte[1024] or less each time.
I've got a answer from MS. The stream is sent by 16ko/32ko chrunk. This behavior is "by design".
More info: http://social.msdn.microsoft.com/Forums/en/wcf/thread/0662dc2e-6468-47c5-9676-fa14a56121cb
精彩评论