[Note: all the information was gathered based on a close-to-Beta 1 build of .NET Framework 4.0 and Visual Studio 2010. Details may vary and change]
We already talked about some basics of discovery support in WCF4. Another nice feature in this area are announcements.
If you as a service consumer are interested when service endpoints go on- and offline you can get notified. The only thing to do is to host an AnnouncementService (provided by WCF4) on the consuming side and subscribe to the OnlineAnnouncementReceived and OfflineAnnouncementReceived events.
For this work the service needs to send out announcements messages in the first place. This can be achieved by adding
an announcement endpoint:
<configuration>
<system.serviceModel>
<services>
<service name="HelloService"
behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="IHelloService" />
<endpoint name="udpDiscovery" kind="udpDiscoveryEndpoint"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceDiscovery>
<announcementEndpoints>
<endpoint name="udpAnnouncement" kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
This is all that needs to be done on the service host side. Remember that the udpAnnouncementEndpoint is one of the new standard endpoints in WCF4.
Consumers can then use the approach outlined in the introduction of this post. I.e. creating an AnnouncementService:
var announcementService = new AnnouncementService();
announcementService.OnlineAnnouncementReceived +=
new EventHandler<AnnouncementEventArgs>(announcementService_OnlineAnnouncementReceived);
announcementService.OfflineAnnouncementReceived +=
new EventHandler<AnnouncementEventArgs>(announcementService_OfflineAnnouncementReceived);
announcementServiceHost = new ServiceHost(announcementService);
announcementServiceHost.AddServiceEndpoint(new UdpAnnouncementEndpoint());
announcementServiceHost.Open();
In the registered event handlers we can then check whether this is actually an announcement for the contract we are interested in:
private void announcementService_OnlineAnnouncementReceived(object sender, AnnouncementEventArgs e)
{
var contractXQN = GetContractXmlQualifiedName();
if (e.AnnouncementMessage.EndpointDiscoveryMetadata.ContractTypeNames.Contains(contractXQN))
{
FireOnlineEvent();
}
}
private void announcementService_OfflineAnnouncementReceived(object sender, AnnouncementEventArgs e)
{
var contractXQN = GetContractXmlQualifiedName();
if (e.AnnouncementMessage.EndpointDiscoveryMetadata.ContractTypeNames.Contains(contractXQN))
{
FireOfflineEvent();
}
}
private static XmlQualifiedName GetContractXmlQualifiedName()
{
var contract = ContractDescription.GetContract(typeof(IHelloService));
var contractXQN = new XmlQualifiedName(contract.Name, contract.Namespace);
return contractXQN;
}
If we now combine online/offline announcements with full metadata-driven discovery we can build sophisticated service-oriented peer solutions.
This is very powerful stuff!
That's it for now for a first introduction to discovery announcements in WCF4.
Stay tuned for more.
Comments