Developing a real-time data flow and control model with WCF

A Windows Communication Foundation (WCF) service is defined through its operations and data contracts. One of the major benefits of WCF is the ease with which a client can create and use these services through the automatically generated proxy classes. The service side is only half of the communications link though. Discovering the correct WCF configuration options that allow a solution to operate properly was not as easy as I thought it would be.

This post describes a specific WCF-based data control and streaming architecture. The primary goal of this service is to provide a continuous data stream (as buffers of short values) from a real-time data acquisition source. The client would then be able to display the data as it became available or store the data when directed by the user. In addition, the service allows the client to both get status information (Getters) and control certain attributes (Setters) of the underlying data source. This is illustrated here:

Real-time architecture

The DataBufferEvent is defined as a one-way callback and continuously delivers data to the client. The IsOneWay property is valid for any operation that does not have a return value and improves network performance by not requiring a return message. The Getters and Setters [for you Java folks, this has nothing to do with JavaBeans] can be called at any time. Changing a data source attribute with a Setter will probably affect the data stream, but it is the responsibility of the data source to ensure data integrity. The underlying transport binding must support duplex operation (e.g. wsDualHttpBinding or netTcpBinding) in order for this scenario to work.

Here is what an example (a sine wave generator) service interface looks like:

The service class is implemented as follows:

The InstanceContextMode.PerSession mode is appropriate for this type of interface. Even though there is probably only a single data source, you still want to allow multiple service session instances to provide data simultaneously to different clients. The data source would be responsible for managing the multiple data requesters.

With the service side complete, all the client needs is to do is create the proxy classes (with either Visual Studio or Svcutil), setup the DataBufferEvent callback and call the appropriate control functions. My first client was a Winform application to display the data stream. The problem I ran into is that even though the data callbacks worked properly, I would often see the control functions hang the application when they were invoked.

It took quite a bit of searching around before I found the solution, which is here. You can read the details about the SynchronizationContext issues, but this caused me to spin my wheels for several days. The upside is that in trying to diagnose the problem I learned how to use the Service Trace Viewer Tool (SvcTraceViewer.exe) and the Configuration Editor Tool (SvcConfigEditor.exe, which is in the VS2005 Tools menu).

So after adding the appropriate CallbackBehavior attributes, here are the important parts of the client that allow this WCF model to operate reliably:

The first take-away here is that WCF is a complex beast. The second is that even though it's easy to create proxy classes from a WCF service, you have to understand and take into account both sides of the communications link. It seems so obvious now!

That's it. This WCF component is just part of a larger project that I'm planning on submitting as an article (with source code) on CodeProject, someday. If you'd like the get a copy of the source before that, just let me know and I'll send you what I currently have.

Update: Proof of my first take-away: Callbacks, ConcurrencyMode and Windows Clients. Thanks Michele! 🙂

This entry was posted in .NET, Networking, WCF. Bookmark the permalink.

9 Responses to Developing a real-time data flow and control model with WCF

  1. Hi Bob
    Interesting and educating article you have published.
    I am more than happy if I could get a copy of the source code, so I can start learning your ideas.

    Best Regards
    /Roger

  2. Pingback: Bob on Medical Device Software » Blog Archive » A .NET code sample: Real-time data streaming and control.

  3. Pete says:

    Bob,
    I would certainly like the code so far. Do you have a complete test system both client and server??
    Thanks

  4. duo says:

    Hello. Is the article on codeproject yet? If not, can i get a copy? ;]

  5. Murali says:

    Hi I am working on the similar kind of application and want to use your ideas. can i get a copy of your code?

  6. Shweta says:

    Hi I am working on the similar kind of application. can i get a copy of your code?

  7. Joshua says:

    Hi Bob,
    Thanks for the article, I would be interested in getting a copy of your code. I am in the beginning stages of experimenting how the WCF works.

  8. Ramya says:

    Hi Bob,

    Thanks for this useful article. Please consider sending me any useful wcf code VS2010 for my self learning.

Leave a Reply

Your email address will not be published. Required fields are marked *