WCF Duplex Bi-directional Streaming with WebSocket Transport

WCF Duplex Bi-directional Streaming with WebSocket Transport

Rate This
  • Comments 4

DuplexBiDirectionalStreaming VS2012 project DuplexBiDirectionalStreaming.zip

Introduction

With WebSocket transport, it’s possible to use streamed transport on the Duplex Callback – something that was previously a technical limitation with WCF but is now possible using this transport. New to .Net4.5 is the NetHttpBinding (and NetHttpsBinding), which is a new standard binding that leverages the WebSocket transport if it makes sense to do so. You can configure it to always use WebSocket transport, but by default, it will only use it for Duplex channel shapes. Why? Well, what this transport gives you is the ability for the server to push data to the client without an incoming request. If your contract is already request-reply, there isn’t much increase in efficiency by using the WebSocket transport. But with duplex, there isn’t necessarily a 1-1 correspondence with requests and replies. Of course, the binding does give you the ability to change this setting.

Requirements

WebSocket transport is only possible on the Windows8 operating system. I’m using Windows8 for this demo with Visual Studio 2012. IIS needs to be installed, with full support for Http Activation and WebSocket Transport. To run the service in IIS from Visual Studio, you need to run Visual Studio as an administrator.

Walk-Through

First, I created a new Console Project in Visual studio. This will be the client. I’ll leave it alone for now, and add a WCF service:

clip_image002

Next, I’ll define the service contract. I want to be able to upload bytes for as long as I want. Also, I want to be able to download bytes at the same time, and have a way to tell the service to stop sending me bytes. I’ll also put something in there to grab logs from the service implementation. I created this in a new class library so it can be shared between the client and service.

Code Snippet
  1. [ServiceContract(CallbackContract = typeof(IClientCallback))]
  2. public interface IStreamService
  3. {
  4.     [OperationContract(IsOneWay = true)]
  5.     void UploadStream(Stream stream);
  6.  
  7.     [OperationContract(IsOneWay = true)]
  8.     void StartDownloadingStream();
  9.  
  10.     [OperationContract(IsOneWay = true)]
  11.     void StopDownloadingStream();
  12.  
  13.     [OperationContract(IsOneWay = true)]
  14.     void DownloadLog();
  15. }

Remember, this is duplex, so all the service operations return void. Anything that needs to go to the client goes through the callback channel, defined by this:

Code Snippet
  1. public interface IClientCallback
  2. {
  3.     [OperationContract(IsOneWay = true)]
  4.     void ReceiveStream(Stream stream);
  5.  
  6.     [OperationContract(IsOneWay = true)]
  7.     void ReceiveLog(List<string> log);
  8. }

Now, to implement the service. To do this, I’ll need some kind of custom stream implementation. For demonstration purposes, I’ll use a stream implementation that sends random bytes and can be turned on or off manually or configured to send for a given duration. In order to not blow MaxReceivedMessageSize buffers, I’ll make a configurable throttle on it. The code for this FlowControlledStream can be found in the attached project.

See the attached project for the full service implementation, but the new thing here is sending a stream on a callback; previously not possible with WCF. Also in there is the implementation of StopDownloadingStream.

Code Snippet
  1. public void StartDownloadingStream()
  2. {
  3.     log.Add(string.Format("[{0}] StartDownloadingStream Invoked.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond));
  4.     IClientCallback clientCallbackChannel = OperationContext.Current.GetCallbackChannel<IClientCallback>();
  5.     ThreadPool.QueueUserWorkItem(new WaitCallback(PushStream), clientCallbackChannel);
  6. }
  7.  
  8. private void PushStream(object state)
  9. {
  10.     IClientCallback pushCallbackChannel = state as IClientCallback;
  11.     localStream = new FlowControlledStream();
  12.     localStream.ReadThrottle = TimeSpan.FromMilliseconds(500);
  13.  
  14.     pushCallbackChannel.ReceiveStream(localStream);
  15. }
  16.  
  17. public void StopDownloadingStream()
  18. {
  19.     log.Add(string.Format("[{0}] StopDownloadingStream Invoked.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond));
  20.     localStream.StopStreaming = true;
  21. }

With that finished, I need to modify the Web.config that was generated for me. I want to use WebSocket Transport, so I’ll use the standard NetHttpBinding. By default, netHttpBinding will use WebSocket transport if the contract is duplex, so I shouldn’t have to explicitly add that. But I will increase the MaxReceivedMessageSize to a ridiculous value. So, I replaced the generated System.ServiceModel section with the following:

Code Snippet
  1. <system.serviceModel>
  2.   <behaviors>
  3.     <serviceBehaviors>
  4.       <behavior name="MyServiceBehavior">
  5.         <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
  6.         <serviceDebug includeExceptionDetailInFaults="true"/>
  7.       </behavior>
  8.     </serviceBehaviors>
  9.   </behaviors>
  10.   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  11.   <bindings>
  12.     <netHttpBinding>
  13.       <binding name="MyBinding" maxReceivedMessageSize="67108864" transferMode="Streamed">
  14.       </binding>
  15.     </netHttpBinding>
  16.   </bindings>
  17.   <services>
  18.     <service behaviorConfiguration="MyServiceBehavior" name="DuplexService.StreamingService">
  19.       <endpoint address="" contract="CommonArtifacts.IStreamService" binding="netHttpBinding" bindingConfiguration="MyBinding" />
  20.     </service>
  21.   </services>
  22. </system.serviceModel>

So, after that, I open the service’s Properties page, go to the Web section, and make sure “Use Local IIS Web server” is selected, and I uncheck “Use IIS Express”. When I save that, and allow VS to create the virtual directory, I can browse to the .svc file and get the standard WCF Service help page. Time to work on the Client code.

The goal for the client is to prove that we are doing true bi-directional streaming. To do this my client will execute the following:

1. Invoke a service operation to start downloading a stream.

2. Signal that bytes are being received.

3. Upload a stream for a few seconds.

4. Signal to the service to stop sending bytes to the receiver.

5. Wait for the end of the stream.

If the service can’t process the incoming stream while sending bytes out, then it will deadlock. The program is in the attached zip file, but here’s a snipped version:

Code Snippet
  1. ClientReceiver receiver = new ClientReceiver();
  2.  
  3. string address = "http://localhost/DuplexService/DuplexService.svc";
  4. NetHttpBinding binding = new NetHttpBinding();
  5. binding.MaxReceivedMessageSize = 64 * 1024 * 1024;
  6. binding.TransferMode = TransferMode.Streamed;
  7. DuplexChannelFactory<IStreamService> factory = new DuplexChannelFactory<IStreamService>(new InstanceContext(receiver), binding, address);
  8. IStreamService client = factory.CreateChannel();
Code Snippet
  1. client.StartDownloadingStream();
  2.  
  3.  Console.WriteLine("[{0}] Waiting for the receiver to start reading bytes.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond);
  4.  receiver.ReceiveStreamInvoked.WaitOne();
  5.  
  6.  Console.WriteLine("[{0}] Client Invoking UploadStream, while the receiver is receiving bytes.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond);
  7.  FlowControlledStream uploadStream = new FlowControlledStream();
  8.  uploadStream.ReadThrottle = TimeSpan.FromMilliseconds(500);
  9.  uploadStream.StreamDuration = TimeSpan.FromSeconds(5);
  10.  client.UploadStream(uploadStream);
  11.  
  12.  Console.WriteLine("[{0}] Client Invoking StopDownloadingStream.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond);
  13.  client.StopDownloadingStream();
  14.  
  15.  Console.WriteLine("[{0}] Waiting on ReceiveStreamCompleted from the receiver.", DateTime.Now.Minute + ":" + DateTime.Now.Second + "." + DateTime.Now.Millisecond);
  16.  receiver.ReceiveStreamCompleted.WaitOne();

Running it produces the following log:

[45:21.308] Client Invoking StartDownloadingStream.

[45:21.844] Waiting for the receiver to start reading bytes.

[45:22.861] ReceiveStream invoked.

[45:22.862] Client Invoking UploadStream, while the receiver is receiving bytes.

[45:27.873] Client Invoking StopDownloadingStream.

[45:27.873] Waiting on ReceiveStreamCompleted from the receiver.

[45:28.371] ReceiveStream read 725248 bytes.

[45:28.371] Getting the log from the server.

The following are the logs from the server:

[45:21.854] StartDownloadingStream Invoked.

[45:23.866] UploadStream Invoked.

[45:27.873] UploadStream Read 528640 bytes.

[45:27.874] StopDownloadingStream Invoked.

[45:28.373] DownloadLog Invoked.

Comparing the server log and the client log shows that server read bytes from the client for about 5 seconds while sending bytes to the client at the same time. I made sure to set the ReadThrottle to the same amount on both client and service, so the client should read more bytes than the service, which it does.

Leave a Comment
  • Please add 6 and 1 and type the answer here:
  • Post
  • hey, can you point with a realworld example of downloading and showing a youtube video or netflix video.

  • Only available on Windows 8? That's highly unfortunate.

  • Yeah - support for WebSocket transport is currently limited to Windows8.  Technically, only server-side support is limited to Windows8.  It's not as if we "shut it off" on downlevel on purpose; it simply can't be done on the operating system.  There's a lot of feedback about this and probably a better response, but that's kind of the way it is.

    But if all you want to do is return a stream, you can use request-reply.  This is just showing the use of a Duplex contract.  Which is nice, if you actually want to push to a client without the client making the request.

  • I don't know the youtube or netflix APIs, but if you wanted to, for some reason, stream to a WCF client using WebSockets on the Duplex callback channel, you could basically modify this demo code, and change the implementation of the Read method in the custom stream to use the bytes for your video.  Or, if YouTube offers a stream object, you could simply return that and do away with the custom stream implementation.

    Search around and you can find demo code of getting bytes from YouTube videos out there; there's nothing specific to WCF about that, so I'm opting to not post about it.  (i.e. there are others who are better at this kind of thing.)

Page 1 of 1 (4 items)