Asynchronous Streaming in WCF

I work with Streaming with WCF, and I have a question about what the paragraph on " Enabling asynchronous streaming " means from the MSDN article on Big Data and Streaming in WCF.

To enable asynchronous streaming, add the DispatcherSynchronizationBehavior endpoint behavior for the host service and set the AsynchronousSendEnabled property to true . We have also added the ability to true asynchronous streaming on the sending side. This improves the scalability of the service in scenarios where it streams messages to multiple clients, some of which are slow in possibly due to network congestion or not read at all. In these scenarios, we no longer block individual service flows per customer. This ensures that the service will be able to handle many clients, thereby improving the scalability of the service.

I understand that this means that I am adding

 <behaviors> <endpointBehaviors> <behavior name="AsyncStreaming"> <dispatcherSynchronization asynchronousSendEnabled="true" /> </behavior> </endpointBehaviors> ... 

To my web.config and referring to the behavior of AsyncStreaming at my endpoint, however I do not understand what these steps do for me. Do I need to modify the code at all to take advantage of this asynchrony?

Also on a similar topic (but if it is too different, I will transfer it to a new question), how to use the async / await effect using Streams in WCF? Can I make Task<Stream> Foo() in my service contract? I am making some database calls, the results of which are eventually transferred to the user stream, which I will return from the WCF service. Being able to use things like ExecuteDataReaderAsync() is very useful, can I use it when working with streaming rather than buffered messages?

I tested it, and I know that it "works" using "Tasks", but I do not know if this will cause the function to return to the "Buffering" mode, for example, when you provide functions with more than one parameter (see 3rd paragraph " " Programming Model for Streaming Broadcasts "on the same MSDN page), and I don’t know how to check if this is happening.

+6
source share
2 answers

I traced it to RequestContext through the .NET source. Apparently, the ChannelHandler.sendAsynchronously field determines whether the message response will be executed asynchronously (via RequestContext.BeginReply/EndReply APM methods) or synchronously through RequestContext.Reply .

As far as I can tell, all this frees up a server-side stream that returns to the pool and which otherwise would be busy inside RequestContext.Reply with the "pumping" of the stream to the client, for how long since the Stream object has been living on the server.

This seems completely transparent, so I think you can safely use async TAP-based contract methods and return a Task<Stream> . For example, in another contract you can make await Stream.WriteAsync .

Please share your experience as your own answer, when you get there, I am very interested in the details :)

+2
source

Good question on a complex topic. I implemented a WCF streaming service to host huge (up to 2 GB) downloads, but I'm also a little confused about this AsyncStreaming = True, since WCF is already asynchronous (in that each connected client receives its own stream and requests and receives asynchronously) until as long as

  <ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple, InstanceContextMode:=InstanceContextMode.PerCall)> 

But you need to change your code to work with streaming. Even if you have Binding.TransferMode = TransferMode.Streamed , the program will return to buffering if you do not change the code so that your load and load functions A) receive and return streams, and B) your load and load functions implements something like of this:

 //oBuffer is your content if (oBuffer != null) { oStream = new MemoryStream(oBuffer); if (oStream.CanSeek) { oStream.Seek(0, SeekOrigin.Begin); } return oStream; } 

This is a decent HowTo article I used as a guide: http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP

+2
source

All Articles