Consuming WCF Pub Sub Service to BroadCast Message Using C#: Part 2

Introduction

My previous article (WCF With Publisher and Subscriber Pattern: Part 1) explained how to develop a WCF pub sub service, here I will explain how to consume that service to broadcast your message out to the world (Subscriber clients). Consuming this type of service is very easy but a little different from consuming a normal WCF Soap service. Download the source code and host it in your local computer. I have also attached the source code of the WCF pub sub service with this article. Here I will create an application that will broadcast messages using a service to clients (out to the world), this type of application is called a publisher in the pub sub pattern of WCF.

Getting Started
 
Open your Visual Studio, create a new WPF project, here I named my project "MessageSender". Add reference of the WCF service to your project. Add the reference of the web service by right-clicking on the project then click on Add Service Reference.
 
Create a class named ServiceCallback and inherit the IMessageCallback Interface of the WCF service and implement the interface by right-clicking on the interface. The ReceivedMessage function is for receiving messaged from the service but this function has no need of a client like this that is for sending messages. This will use full for the client for receiving messages. The class will be like the following.
  1. namespace MessageSender  
  2. {  
  3.     public class ServiceCallback : MessageSender.MessageBroaker.IMessangerCallback  
  4.     {  
  5.         public void ReceivedMessage(string Name)  
  6.         {  
  7.             MessageBox.Show(Name);  
  8.         }  
  9.     }  
  10. }  
 
Add a text block control into the main window, set the text property to "Message" and set their position. Use a text box control to set the name to whatever you want, here I have given "tbkMessage" and again set the position for a good look. Again use a button control, set the text to "Send" and name it "btnSend". The code for the structure of the main window is given below. 
  1. <Window x:Class="MessageSender.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="350" Width="525">  
  5.     <Grid>  
  6.         <TextBlock HorizontalAlignment="Left" Margin="10,31,0,0" TextWrapping="Wrap" Text="Message" VerticalAlignment="Top"/>  
  7.         <TextBox HorizontalAlignment="Left" Height="23" Margin="62,28,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="281" Name="tbkMessage"/>  
  8.         <Button Name="btnSend" Content="Send" HorizontalAlignment="Left" Margin="348,29,0,0" VerticalAlignment="Top" Width="75" Click="btnSend_Click"/>  
  9.   
  10.     </Grid>  
  11. </Window>  
On the click event of the send button, create an object of the InstanceContext class under System.ServiceModel and pass the object of the class ServiceCallback to the constructur as the parameter. This class helps to open a socket connection of the service. 
  1. ServiceCallback SCB = new ServiceCallback();  
  2.            context = new InstanceContext(SCB);  
Create the object of the client class of service, here the client class is MessangerClient and pass the object of the context to the contructor as a parameter.

Then call the PublishMessage function of MessangerClient to broadcast your message to the outside world. The PublishMessage function of the class MessangerClient takes a string  parameter as the message to broadcast.
  1. ServiceCallback SCB = new ServiceCallback();  
  2.            context = new InstanceContext(SCB);  
  3.            MessageBroaker.MessangerClient MC = new MessageBroaker.MessangerClient(context);  
  4.            MC.PublishMessage("Hi");  
The preceding is how to create an application that consumes a WCF Pub Sub Service for broadcasting a message to the outside world. In my next article I will write about "How  to Consume WCF Pub Sub Service to receive message: Part 3".
Summary

I hope you have learned how to consume a WCF Pub Sub Service to send a message (Broadcast Message) to subcriber clients. 
 

Up Next
    Ebook Download
    View all
    Learn
    View all