Pub sub and WCF discovery in framework 4.0
I have implemented many pub sub infrastructures for many customers. I found out that one of the major rezones they used the pub sub infra was to be able to tell that a service just went up.
Now with WCF 4.0 all this pub sub infrastructure we built is not needed any more. A service can use WS Discovery messages and publish the fact that it is up. Simple and clean!
The question is what about other events?
Well you can always keep using the pub-sub infra but I am looking for a cheaper solution.
I personally think that an infrastructure should be built only when there is a really good reason.
There are two major alternatives
1. Soon the .Net services will be released. They offer a pub-sub functionality using the .NetEventRelay binding. In future posts I will speak more about the .Net services.
2. Another alternative is to use dummy services and WS discovery messages.
The idea is simple: For each event you want to publish just create a dummy service that do nothing. When the event occurs bring it up and use WS discovery to spread the message. The consumers will know to interpret that this dummy service notification actually means that a business event had happened. After some time bring the dummy service down.
Loading a service and killing it has a performance price but for rare events this can be a nice solution that saves the need to build a whole infrastructure.
So what do you need to do to notify the world that you are up using WS Discovery?
1. Add the following behavior and make sure your service uses it. You can of course always add this behavior to an existing one:
<behavior name="WSDiscoveryBehavior">
<serviceDiscovery>
<announcementEndpoints>
<endpoint name="udpEndpointName"
kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
2. All those who wants to listen to the notifications should use an announcement service.
So create an instance of such and register to its events
this.myAnnouncementService = new AnnouncementService();
// Register to the events
this.myAnnouncementService.OnlineAnnouncementReceived +=
new EventHandler<AnnouncementEventArgs>(this.OnlineHandler);
3. Host the announcement service to create listening endpoint for the Online (Hello) and Offline (Bye) announcement messages.
this.myAnnouncementServiceHost = new ServiceHost(this.announcementService);
// Add the announcement endpoint
this.myAnnouncementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
this.myAnnouncementServiceHost.BeginOpen(
(asynResult) =>
{
myAnnouncementServiceHost.EndOpen(asynResult);
},
null);
}
In the handler you have to make sure you get the right notification from the right service
private void OnOnlineAnnouncement(object sender, AnnouncementEventArgs e)
{
EndpointDiscoveryMetadata discoveryMetadata =
e.AnnouncementMessage.EndpointDiscoveryMetadata;
FindCriteria criteria =
new FindCriteria(typeof(IDummyService));
if (criteria.IsMatch(discoveryMetadata))
{
...
}
}
That is it.
Enjoy
Manu