Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C#
Tip/Trick

How to implement subcription based on WCF

Rate me:
Please Sign up or sign in to vote.
4.50/5 (6 votes)
15 Feb 2011CPOL2 min read 22.1K   12   4
How to implement subcription based on WCF

Introduction


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.



Background


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


C#
//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();
}
[ServiceContract]
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:
http://www.codeproject.com/KB/WCF/How_to_abstract_WCF.aspx
http://www.codeproject.com/KB/WCF/WCF_ESB.aspx

License

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


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Nilpesh10-Jul-12 0:40
Nilpesh10-Jul-12 0:40 
GeneralRe: My vote of 2 Pin
WillCaptain15-Jan-13 3:38
WillCaptain15-Jan-13 3:38 
GeneralReason for my vote of 5 Seems very useful!! Pin
Scarlettlee21-Feb-11 20:59
professionalScarlettlee21-Feb-11 20:59 
GeneralWhere did you upload the image? To another article? It appea... Pin
Indivara9-Feb-11 1:55
professionalIndivara9-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.