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

The Service code consists of an Interface (IMagazineService) and its implementation (MagazineService).

The Interface defines all the capabilities of the service. What do we really want from a Publisher service?

  1. A Publish method (called PublishMagazine)
  2. A Subscribe method (to allow clients to subscribe to the service)
  3. An Unsubscribe method (to allow clients to unsubscribe from the service)

Those are the three main things we need from our Publisher service. As long as all we need is the Publisher to get in touch with multiple subscribers, we are all set.

However, we also need a way to notify individual clients (subscribers) who have subscribed to our service. 

This can be handled via a Callback method (called MessageReceived in our example). The Callback method is best made available as part of a separate interface (known as IClientContract).

public interface IClientContract

{

    [OperationContract(IsOneWay = true)]

    void MessageReceived(string hyperlinkToNewIssue, string issueNumber, DateTime datePublished);

}

[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IClientContract))]

public interface IMagazineService

{

    [OperationContract(IsOneWay = false, IsInitiating = true)]

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

    [OperationContract(IsOneWay = false, IsTerminating = true)]

    void Subscribe();

    [OperationContract(IsOneWay = false, IsTerminating = true)]

    void Unsubscribe();

 

}

 

public interface IClientContract

{

    [OperationContract(IsOneWay = true)]

    void MessageReceived(string hyperlinkToNewIssue, string issueNumber, DateTime 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, Hands-On Technology Architect, Clean Air Activist.


Series NavigationWCF and Publish Subscribe– A Full Example: IntroductionWCF Publish Subscribe– A Full Example: The Service Side Part 2 (Implementation)