Click here to Skip to main content
13,055,408 members (54,589 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 9 Feb 2011

How to implement subcription based on WCF

, 15 Feb 2011
Rate this:
Please Sign up or sign in to vote.
How to implement subcription based on WCF


The first question is what is Synchronized Distributed System? I'm not sure of the official term. I named it from my understanding. The typical application is stock trade software. If you have experience on stock investment, you will like the user interface in front of you could display all the changing information of the stock you are staring at just in time. But to be honest, I hate Chinese stock market as well as the real estate market.

In this part, we are going to simulate a similar scenario: Inventory Monitor. I design two persons whose names are Will, Mike respectively. They are in different offices. Will and Mike are all responsible for inventory management. If Will adds one product into system, Mike should be able to see the new product. If Mike changes the inventory number, Will is able to see the change as well.


My final purpose is to design a service bus mechanism based on WCF. This article would be the first part.

Using the Code

It is actually not difficult to make a remote call. The key point to make a Synchronized system is to implement subscription from server. To implement subscription, we need to user duplex connection

//session mode ensure in case client connects server, the connection will not break
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IMessageSenderCallBack))]
public interface IMessageSender
{//init is for first time client connecting server
 [OperationContract(IsOneWay = false, IsInitiating = true, IsTerminating = false)]
 void Init();
public interface IMessageSenderCallBack
  [OperationContract(IsOneWay = true)]
  void SendMessage(Message message);
  [OperationContract(IsOneWay = true)]
  void SendHeartBeat();

The subscription in WCF is a little bit like what we called observer design pattern.

SessionMode = SessionMode.Required 

Help to keep the connection between server and client

CallbackContract = typeof(IMessageSenderCallBack))

Open the interface to client to enable server to call the method implemented IMessageSenderCallBack

[OperationContract(IsOneWay = true)]

indicate server will call the client methods without waiting for the return value.

From the client side, we create a class implemented from IMessageSenderCallBack. In our case, the main form is the class.

*Now we draw the scenario on how to change inventory and let other clients know the change.

Points of Interest

Event is very important to help you build up the subscription system. On server side, we don't know how to trigger the subscription to multi-client. That is why we have an Init() method in MessageSender class. Init() will register those events needs to be subscribed.

How to demonstrate the code

  1. Open SAOT.exe

  2. Click Confirm to open the service

  3. Open ClientTest.exe

  4. Click Confirm to connect the server

  5. Open another ClientTest.exe instance

  6. Connect the server as well. Change the name from Will to Mike

  7. Now you have 2 clients.

Please also read the following articles:


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


About the Author

China China
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Nilpesh10-Jul-12 0:40
memberNilpesh10-Jul-12 0:40 
GeneralRe: My vote of 2 Pin
WillCaptain15-Jan-13 3:38
memberWillCaptain15-Jan-13 3:38 
GeneralReason for my vote of 5 Seems very useful!! Pin
Scarlettlee21-Feb-11 20:59
memberScarlettlee21-Feb-11 20:59 
GeneralWhere did you upload the image? To another article? It appea... Pin
Indivara9-Feb-11 1:55
subeditorIndivara9-Feb-11 1:55 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 16 Feb 2011
Article Copyright 2011 by WillCaptain
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid