WCF Publish Subscribe–A Full Example – The Service Side Part 1 (Interface)
- WCF and Publish Subscribe– A Full Example: Introduction
- WCF Publish Subscribe–A Full Example – The Service Side Part 1 (Interface)
- WCF Publish Subscribe– A Full Example: The Service Side Part 2 (Implementation)
- WCF and Publish Subscribe–A Full Example: The Event Generator
- WCF and Publish Subscribe–A Full Example: Client Code
- WCF and Publish Subscribe–A Full Example: Running the Client (Subscriber)
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?
- A Publish method (called PublishMagazine)
- A Subscribe method (to allow clients to subscribe to the service)
- 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);
}
Note –
There are problems with this code …
PublishMagazine should have this contract: [OperationContract(IsOneWay = false)]
Subscribe should have this contract: [OperationContract(IsOneWay = false, IsInitiating = true)]
Thanks Seth.