Consuming WCF Pub Sub Service to Receive Message Using C#: Part 3


In my previous two articles I explained how to create a WCF service with the Pub Sub pattern and consume this service to send messages to the outside world (Publisher Clients). This article shows how to create client (Subscribed) to receive messages sent by a publisher client using a WCF Service.
Previous Articles
  1. WCF With Publisher and Subscriber Pattern: Part 1 
  2. Consuming WCF Pub Sub Service to BroadCast Message Using C#: Part 2
Getting Started

Download the service part, host in your local server or local computer if IIS available. Open your Microsoft Visual Studio, create a new WPF application, here I have given the name of this project "MessageReceiver", get the reference of the service.
After creating the project, you will get the first window name MainWindow. Use a text block control and list box control in the main window, reset their position in the following way given in the XAML.  
  1. <Window x:Class="MessageReceiver.MainWindow"  
  2.         xmlns=""  
  3.         xmlns:x=""  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <DockPanel LastChildFill="True">  
  6.         <TextBlock DockPanel.Dock="Top" Height="26" TextAlignment="Center" Text="Messages"></TextBlock>  
  7.         <ListBox Name="lbxMessages"></ListBox>  
  8.     </DockPanel>  
  9. </Window>  
Declare an event that will help to get a received message for your list controls. Here I have given the name of this event as "MessageReceivedEvent". See the following code for event details.
  1. public delegate void MessageReceivedHandler(string messages);  
  2.         public static event MessageReceivedHandler MessageReceivedEvent;  
Now create a class, here in this demo I have given the name "ServiceCallback" to the class. Inherit the interface "IMessangerCallback" of the service and implement this interface. After implimenting this interface you will get ReceivedMessage. This function has a string parameter that represents a message. The ReceivedMessage function plays a major role for this application. The function receives a message from the service. This function then calls the MessageReceiveEvent to send data to the MainWindow class where your list controls are available to display/list the messages.
The following is the code of the class:
  1. namespace MessageReceiver  
  2. {  
  3.    public class ServiceCallback:MessageBroaker.IMessangerCallback  
  4.     {  
  5.         public void ReceivedMessage(string message)  
  6.         {  
  7.             MainWindow.MessageReceivedEvent(message);  
  8.         }  
  9.     }  
  10. }  
 Now for the MainWindow class. In the contrurctor of this class, provide a functuon to the MessageReceivedEvent event. 
  1. public MainWindow()  
  2. {  
  3.     InitializeComponent();  
  4.     MainWindow.MessageReceivedEvent += MainWindow_MessageReceivedEvent;  
  5. }  
Again create an object of  ServiceCallback that will be passed as a parameter to the InstanceContext class. Declare a class label object of InstanceContext and intiate the object in the constructor of the MainWindow class. The InstanceContext  class is for opening a socket connection. Create an object of MessangerClient of the service, it takes context as a parameter. Call the subscribe function of MessageClient to connect with the service.
  1. ServiceCallback SCK = new ServiceCallback();  
  2. Context = new InstanceContext(SCK);  
  3. MC = new MessageBroaker.MessangerClient(this.Context);  
  4. MC.Subscribe();  
For  the MainWindow_MessageReceivedEvent function, write code to list the messages in a listbox (lbxMessages) as in the following:
  1. void MainWindow_MessageReceivedEvent(string messages)  
  2. {  
  3.     this.lbxMessages.Items.Add(messages);  
  4. }  
In the close event of MainWindow call Unsubscribe to disconnect from the service as in the following:
  1. private void Window_Closed(object sender, EventArgs e)  
  2. {  
  3.     MC.Unsubscribe();  
  4. }  

The preceding is the complete code for creating a client (Subscriber)  to receive messages in the WCF Pub Sub pattern. I hope you have learned how to create this.