Click here to Skip to main content
Click here to Skip to main content

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

, 1 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

    // 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:

    //----------------------------------------------------------------------------------
    /// <span class="code-SummaryComment"><summary></span>
    /// Callback contract to update clients with TickUpdate data stream.
    /// <span class="code-SummaryComment"></summary></span>

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

    //-----------------------------------------------------------------------------------
    /// <span class="code-SummaryComment"><summary></span>
    /// WCF Contracts for client interaction with this service
    /// <span class="code-SummaryComment"></summary></span>
    [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)

Share

About the Author

Carlos A Alvarez
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

 
Questionconect-reconnect on failure? PinmemberMikeKosak16-Oct-11 6:18 
AnswerRe: conect-reconnect on failure? PinmemberCarlos A Alvarez17-Oct-11 4:01 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 1 Oct 2008
Article Copyright 2008 by Carlos A Alvarez
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid