Click here to Skip to main content
15,891,316 members
Articles / Programming Languages / C#
Article

Simple Real-Time Ticker Using C# / NET / WCF / WPF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (23 votes)
1 Oct 2008CPOL2 min read 141.2K   4.6K   105   19
This article is a basic working sample of using WCF and WPF to publish real-time data.

Introduction

This is a simple working Real-Time data publisher written in C# NET 3.5. It is composed of two pieces, a server WCF console and a WPF client. It demonstrates a simple way of maintaining a persisted callback to multiple clients. Each client subscription is maintained by the server console.

The code demonstrates an implementation of the following:

  • Publish / Subscribe Implementation (not events)
  • ClientCallback subscription
  • Fake Tick Plant
  • Data Dictionary
  • Multi-Threading
  • Object Serialization

Overview Class Structure

How to Run

There are two working executables in this project, a WCF console (WCFMarketDataServer.exe) and a WPF client (WFCTickerWindow.exe).

First start the WCF console. You can only have one instance running. The server is ready once you see the "press any key" prompt:

WCF Console

Now you can start the client. You can start as many clients as you want to simulate multiple users. Each client should maintain its own set of real time data.

WPF Client

As you change the preset value on the combo boxes, a subscription / un-subscription requests will be processed by the server to maintain the proper data each to which each client is subscribed.

Using the Code

Below, find an overview of the main class structure. I am not going into much detail but it shows how the main data flows and the subscription mechanism is working.

Detailed Class overview

The WCF server console is configured programmatically utilizing a TCP channel.

C#
// Service htpp address
CMarkerConfig.LogInfo("Creating URI...");
Uri baseAddress = new Uri("net.tcp://localhost:8080/MarketData");

// Create new WCF Service host
CMarkerConfig.LogInfo("Creating ServiceHost...");
serviceHost = new ServiceHost(typeof(CMarketDataManager), baseAddress);


// Add the htpp end point
NetTcpBinding pNetTcpBinding = new NetTcpBinding(SecurityMode.Transport);


CMarkerConfig.LogInfo("Adding Endpoint...");
serviceHost.AddServiceEndpoint(typeof(IMarketData), pNetTcpBinding, baseAddress);

// Add the Metadata
CMarkerConfig.LogInfo("Adding Metadata behavior...");
ServiceMetadataBehavior servicemetadatabehavior = new ServiceMetadataBehavior();
servicemetadatabehavior.HttpGetEnabled = true;
servicemetadatabehavior.HttpGetUrl = new Uri("http://localhost:8081/MarketData");
serviceHost.Description.Behaviors.Add(servicemetadatabehavior);

Following is the service contact:

C#
//----------------------------------------------------------------------------------
/// <summary>
/// Callback contract to update clients with TickUpdate data stream.
/// </summary>

interface IClientCallback
{
    //-------------------------------------------------------------
    [OperationContract(IsOneWay = true)]
    void SendTickUpdate(TickUpdate pTickUpdate);
}

//-----------------------------------------------------------------------------------
/// <summary>
/// WCF Contracts for client interaction with this service
/// </summary>
[ServiceContract(Namespace = "tcp://localhost/", CallbackContract = typeof(
    IClientCallback))]
interface IMarketData
{
    //-------------------------------------------------------------
    [OperationContract]
    string[] GetSymbolList();

    //-------------------------------------------------------------
    [OperationContract]
    StringCollection GetDataSourceList();

    //-------------------------------------------------------------
    [OperationContract]
    string[] GetFieldList();

    //-------------------------------------------------------------
    [OperationContract]
    void SubscribeRT(CGenericMsg pCGenericMsg);
}

All data updates are sent via IClientCallback implemented by the WPF client.

The method SubscribeRT() is designed to take a CGenericMsg object. This object is generic so multiple types of messages can be added without having to change the contract.

Points of Interest

This sample helped me better understand what WCF and WPF are all about. It is a good introductory project into the frameworks for an intermediate developer.

I hope someone can benefit from it at some level as much as I did.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Windows Consultant
United States United States
20+ yrs Leading and Developing Microsoft products in the Financial Industry.

My main background is VC++, server and client development.

Currently focused in WPF/XAML, Windows 8 and Windows Azure Server technologies.

Comments and Discussions

 
Questionmissing a reference to WCFMarketDataShared Pin
eli5m3-Aug-13 23:15
eli5m3-Aug-13 23:15 
QuestionExcellent code but poor article explanation Pin
Dav Zen27-Dec-11 0:37
Dav Zen27-Dec-11 0:37 
QuestionMake the client run on android or iOS Pin
Benjamin Krause6-Dec-11 0:38
Benjamin Krause6-Dec-11 0:38 
AnswerRe: Make the client run on android or iOS Pin
Carlos A Alvarez7-Dec-11 6:21
Carlos A Alvarez7-Dec-11 6:21 
GeneralRe: Make the client run on android or iOS Pin
Benjamin Krause7-Dec-11 6:47
Benjamin Krause7-Dec-11 6:47 
Questionconect-reconnect on failure? Pin
MikeKosak16-Oct-11 5:18
MikeKosak16-Oct-11 5:18 
AnswerRe: conect-reconnect on failure? Pin
Carlos A Alvarez17-Oct-11 3:01
Carlos A Alvarez17-Oct-11 3:01 
GeneralCloud Hosted? Pin
nz_ham23-May-11 17:50
nz_ham23-May-11 17:50 
GeneralRe: Cloud Hosted? Pin
Carlos A Alvarez17-Oct-11 2:59
Carlos A Alvarez17-Oct-11 2:59 
Generalsolution is missing reference to WCFMarketDataShared Pin
cybNeo26-Jan-11 8:19
cybNeo26-Jan-11 8:19 
QuestionWould you mind sharing some performance insight? Pin
sdai10-Dec-09 9:37
sdai10-Dec-09 9:37 
AnswerRe: Would you mind sharing some performance insight? Pin
Wayne Walter19-Dec-09 10:41
Wayne Walter19-Dec-09 10:41 
AnswerRe: Would you mind sharing some performance insight? Pin
sdai23-Dec-09 11:07
sdai23-Dec-09 11:07 
GeneralRe: Would you mind sharing some performance insight? Pin
Wayne Walter23-Dec-09 12:50
Wayne Walter23-Dec-09 12:50 
Often the IL code of C# takes less space than a C++ precompiled since many of the IL codes must be translated to multiple machine code instructions. IL is slightly higher level instruction set than most CPUs offer.

If you start to compare C++ to C# in other areas besides just speed, C++ loses completely. The major area are tools that simplify programming including, most notably, automated unit testing. The tools like NUnit and TypeMock that are available for C# for automated testing blow away what you can do in C++. Much of that has to do with C# having reflection features like Java.

So if you're doing anything on Windoze, C# wins, for sure. For Linux, C# runs does on Linux using Mono and is binary compatible. However, therein lies a paradox since M$ isn't too thrilled about Linux, there aren't as good of development IDE's on Linux for C#. But the compiled programs are binary compatible as long as your code either:

1. Never uses any O/S APIs directly <--- which severely limits your ability to get great performance or ..
2. Make a low level library for O/S calls like WinSock2 on WinDoze and Berkely sockets on Linux. So that when you switch O/S, you simply load a different dll wrapper for each O/S.

Finally, if you want a GUI with your app, you have to decide whether to use a graphics library that works on both Linux or Windows which makes your app seam a little lame on Windows or do one of the following.

Smart developers who must support both Windows and Linux but need a great-looking Windows GUI can build their apps either with MVC or MVVM design so that it's easy to "skin" the app and have a Linux friendly skin which can be straightforward if you keep the Skin or "view" code separate from the Model and Controller code and make sure those parts of pure C# and O/S independent.

The other option is our choice at TickZoom and the customers of TZ, that is, build a Windows GUI for monitor and control but all the high speed server component which don't need a GUI can be run on both WinDoze or Linux.

Well, that's my treatise on C# vs C++.

Wayne
AnswerRe: Would you mind sharing some performance insight? Pin
sdai23-Dec-09 14:31
sdai23-Dec-09 14:31 
GeneralThanks for sharing Pin
Member 456543328-Oct-09 10:42
Member 456543328-Oct-09 10:42 
GeneralExcellent Article Pin
gil66618-Oct-09 21:33
gil66618-Oct-09 21:33 
GeneralREAL TIME SCHEDULING SYSTEM Pin
rikki2337527-Oct-09 2:57
rikki2337527-Oct-09 2:57 
GeneralExcellent ! Thanks! Pin
okcode12-Jun-09 19:46
okcode12-Jun-09 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.