Anuj Varma Software • Cloud • Security • AI

WCF Publish Subscribe– A Full Example: The Service Side Part 2 (Implementation)

This entry is part 3 of 6 in the series WCF Publish Subscribe - A Full Example in C#, Step by Step

Our implementation is based on .NET events (for a non-events based implementation, check out the New IObserver IObservable interfaces in C# 4.0). The idea of publishing goes hand-in-hand with ‘raising an event’  – and the idea of subscribers goes hand-in-hand with consuming an event.

Our event based implementation thus consists of:

  1. Defining an event
  2. Defining an  event handler (all the handler will do is notify the various subscribers)
  3. Implement the Subscribe method
  4. Implement the Unsubscribe method
  5. Implement the PublishMagazine method
public class NewIssueAvailableEventArgs : EventArgs

{

    public string hyperlinkURL;

    public string issueNumber;

    public DateTime datePublished;

}

 

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

public class MagazineService : IMagazineService

{

    public static event NewIssueAvailableHandler NewIssueAvailableEvent;

    public delegate void NewIssueAvailableHandler(object sender, NewIssueAvailableEventArgs e);

 

    // instance of eventhandler

    NewIssueAvailableHandler newIssueHandler;

 

    // Callback to individual clients using the ClientContract

    IClientContract callback = null;

 

    public void PublishMagazine(string hyperLinkToIssue, string issueNumber, DateTime datePublished)

    {

        NewIssueAvailableEventArgs eargs = new NewIssueAvailableEventArgs();

        eargs.hyperlinkURL = hyperLinkToIssue;

        eargs.issueNumber = issueNumber;

        eargs.datePublished = datePublished;

 

        // fire the event

        NewIssueAvailableEvent(this, eargs);

    }

 

    public void Subscribe()

    {

        callback = OperationContext.Current.GetCallbackChannel<IClientContract>();

        // this is the handler that we will 'unsubscribe' from later

        newIssueHandler = new NewIssueAvailableHandler(MagazineService_NewIssueAvailableEvent);

        NewIssueAvailableEvent += newIssueHandler;

    }

 

    public void Unsubscribe()

    {

        NewIssueAvailableEvent -= newIssueHandler;

    }

 

    public void MagazineService_NewIssueAvailableEvent(object sender, NewIssueAvailableEventArgs e)

    {

        callback.MessageReceived(e.hyperlinkURL, e.issueNumber, e.datePublished);

    }

}

Anuj holds professional certifications in Google Cloud, AWS as well as certifications in Docker and App Performance Tools such as New Relic. He specializes in Cloud Security, Data Encryption and Container Technologies.

Initial Consultation

Anuj Varma – who has written posts on Anuj Varma.


Series NavigationWCF Publish Subscribe–A Full Example – The Service Side Part 1 (Interface)WCF and Publish Subscribe–A Full Example: The Event Generator

8 comments

    1. I am having the same issue, I think it is missing to invoke the event… I’m struggling: How did you solve it?
      Thanks

  1. Problems with this code – PublishMagazine should have this call at the end: NewIssueAvailableEvent(this, eargs);

  2. Another problem: The Subscribe method should be “+=” on the line: NewIssueAvailableEvent = newIssueHandler; … Otherwise you can only ever have one subscriber.

    1. All I have is a general example of the IObserver and IObservable interfaces. To use them within WCF, I would start with the Service Operation code – and make that class implement the IObservable interface (assuming your WCF service will be the publisher). It would need to implement Subscribe and Remove methods as shown below:

      public class MagazineService : IMagazineService, IObservable
      {

      public IDisposable Subscribe(IObserver observer)
      {
      if (!observers.Contains(observer))
      {
      observers.Add(observer);
      }

      return new RemoveMe(observers, observer);
      }

      private class RemoveMe : IDisposable
      {
      private List> observers;
      private IObserver
      observer;

      public RemoveMe(List> observers, IObserver observer)
      {
      this.observers = observers;
      this.observer = observer;
      }

      public void Dispose()
      {
      if (observer != null && observers.Contains(observer))
      {
      observers.Remove(observer);
      }
      }
      }

      }

  3. Very nice examples. I was however wondering, if you could post an example with iobservable on wcf? would be of great help to understand the technicalities

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.