WCF With Publisher and Subscriber Pattern: Part 1

Introduction
 
Here I will explain how to create a simple WCF service with Pub and Sub patterns. Many authors have explained this pattern in various ways. The procedure in this article is a little different. This type of service is less different than with the default WCF service. So there is nothing to worry about. 
 
Getting Started 
 
Open Microsoft Visual Studio version 2008, 2010 or 2012 and create a new project by clicking the new project option. Select WCF template from the left side tree view. From the right side panel select "WCF Service Application" sub template and rename your project Messanger. After clicking the OK button the project will be opened. Here I will build a service that broadcasts a message to the outside world.
 
new wcfservice 
 
Add another project by right-clicking the main project. Select WCF Template from the left pane then select WCF Service Library from the right pane then rename the project to MessangerLibrary.
 
 
 
Delete the class and interface that has been created by default (Service1 and IService1). Create an interface with the name ICallbackService and declare a function named ReceivedMessage inside the interface as in the following.  
  1. public interface ICallbackService    
  2. {    
  3.     [OperationContract(IsOneWay = true)]    
  4.     void ReceivedMessage(Message message);    
  5. }    
Create another new interface with the name IMessanger. Declare three functions named Subscribe, Unsubscribe and PublishMessage and set the service tags as in the following code.
  1. [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallbackService))]    
  2. public interface IBroadcaster    
  3. {    
  4.     [OperationContract(IsOneWay = false, IsInitiating = true)]    
  5.     void Subscribe();    
  6.     [OperationContract(IsOneWay = false, IsInitiating = true)]    
  7.     void Unsubscribe();    
  8.     [OperationContract(IsOneWay = true)]    
  9.     void PublishMessage(Message message);    
  10. }    
Create a new class named ServiceEventArgs that will inherit the EventArgs class in the System namespace. Then clear a string property with the name of the message inside the class.
  1. public class ServiceEventArgs : EventArgs    
  2. {    
  3.     public string message;    
  4. }    
Add a  WCF Service class to you project, rename the service class to Messanger.
 
 
 
Inherit the IMessanger interface and inherit the functions also. See the following code that explains the details of the Messanger class. 
  1. public class Messanger : IMessanger    
  2. {    
  3.   
  4.    ICallbackService ServiceCallback = null;    
  5.    private static List<ICallbackService> Subscribers = new List<ICallbackService>();    
  6.   
  7.    public void Subscribe()    
  8.    {    
  9.       ServiceCallback = OperationContext.Current.GetCallbackChannel<ICallbackService>();    
  10.       Subscribers.Add(ServiceCallback);    
  11.    }    
  12.   
  13.    public void Unsubscribe()    
  14.    {    
  15.       ServiceCallback = OperationContext.Current.GetCallbackChannel<ICallbackService>();    
  16.       lock (Subscribers)    
  17.       {    
  18.           Subscribers.Remove(ServiceCallback);    
  19.       }    
  20.    }    
  21.   
  22.    public void PublishMessage(string message)    
  23.    {    
  24.       foreach (ICallbackService ICS in Subscribers)    
  25.       {    
  26.           try    
  27.           {    
  28.               if ((((IChannel)ICS).State == CommunicationState.Opened))    
  29.                   ICS.ReceivedMessage(message);    
  30.               else    
  31.                   Subscribers.Remove(ICS);    
  32.           }    
  33.           catch (Exception ex)    
  34.           { }    
  35.       }    
  36.    }    
  37. }    
Code line number 4 creates an object of the ICallbackService interface and assigns null to it. This object identifies the client and keeps his information. Line 5 creates a static list of ICallbackService interfaces to keep a list of active clients. This list will help to send a message to an active client only.
 
The code inside the function Subscriber, lists the instance of the connected client. The Message Receiver client will call this function to connect with this service. When the client calls this function the service keeps the client information in the list. The GetCallbackChannel function of OperationContext in the System.ServiceModel namespace fetches the instance client. 
  1. public void Subscribe()    
  2. {    
  3.     ServiceCallback = OperationContext.Current.GetCallbackChannel<ICallbackService>();    
  4.     Subscribers.Add(ServiceCallback);    
  5. }    
The function Unsubscribe disconnects the client from the service. In this function I removed the instance of the client from the list so that the service will not provide the message to disconnected client. Message Receiver client will call the function when it is time to disconnect from the service. After calling this function the client will not receive any message from the service.
  1. public void Unsubscribe()    
  2. {    
  3.     ServiceCallback = OperationContext.Current.GetCallbackChannel<ICallbackService>();    
  4.     lock (Subscribers)    
  5.     {    
  6.         Subscribers.Remove(ServiceCallback);    
  7.     }    
  8. }    
The PublishMessage function scans the active clients from the list, sends a message and removes the disconnected clients from the list. The Message Sender client will call this function to broadcast a message to the Message Receiver client. 
  1. public void PublishMessage(string message)    
  2. {    
  3.    foreach (ICallbackService ICS in Subscribers)    
  4.    {    
  5.        try    
  6.        {    
  7.            if ((((IChannel)ICS).State == CommunicationState.Opened))    
  8.                ICS.ReceivedMessage(message);    
  9.            else    
  10.                Subscribers.Remove(ICS);    
  11.        }    
  12.        catch (Exception ex)    
  13.        { }    
  14.    }    
  15. }    
In the preceding code line 8 broadcasts messages to service calls using the ReceivedMessage function of the ICallbackService interface that will help the service to deliver a message to the client. Now to explain how it works.
 
Both the service and the client keeps an instance of the Callback service class that inherits CallbackService  and when the service calls this function though the client it also inherits the same interface the function in the clients call. 
 
Note: I will explain that in detail in the next article that will explain the client.
 
Now build your Library project. Go to the Messanger application project and add the reference of the Messanger Library project and build it.
 
Right-click on the Web.config file of Messanger and select Edit WCF Configuration. The following window will open (as in Image 1). Right-click on the Service and select New Service menu. at the right pane click on the button marked in Image 2. The service type browser window will open (as in Image 3). Select the Messanger Library and click the Open button then select the messanger service (Image-4). 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Create endpoint. To create an endpoint use the following procedure. Right-click on endpoints then click on New Service Endpoints.  Set the configs setting applied in the image.
 
 
 
 Delete the Service1.svc class by expanding the Service1.sv and open the service1, make the changes shown in the image. Then build you project.
 
 
 
Your service with pub sub pattern is now ready for hosting. Host your service in IIS. The articles for messander (the client that will send a message to the service) and Message receiver (The client that will receive the sender's message through the service) will be published in the next Part-II and Part-III.
 
Summary
 
I hope you have learned in this how to create a service with a publisher and subscriber pattern.
 
Thanks.


Similar Articles