WCF Publish Subscribe– A Full Example: The Service Side Part 2 (Implementation)
- 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)
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:
- Defining an event
- Defining an event handler (all the handler will do is notify the various subscribers)
- Implement the Subscribe method
- Implement the Unsubscribe method
- 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);
}
}
PublishMagazine missing something… fire event
I am having the same issue, I think it is missing to invoke the event… I’m struggling: How did you solve it?
Thanks
event is not fired !!!!! client is not recieving anything !!!
Problems with this code – PublishMagazine should have this call at the end: NewIssueAvailableEvent(this, eargs);
Another problem: The Subscribe method should be “+=” on the line: NewIssueAvailableEvent = newIssueHandler; … Otherwise you can only ever have one subscriber.
Hi, do you have an example of using IObserver and IObserverable with WCF? Thanks.
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> observers; observer;
{
private List
private IObserver
public RemoveMe(List> observers, IObserver observer)
{
this.observers = observers;
this.observer = observer;
}
public void Dispose()
{
if (observer != null && observers.Contains(observer))
{
observers.Remove(observer);
}
}
}
}
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