WCF Discovery

A new feature in WCF4.0 is buildin Discovery. With Discovery a Hello message is send to the Discovery service when a service starts. This way the Discovery service knows the services is available. Whenever the service stops a Goodbye message is send to notify the Discovery service it is no longer available. Microsoft has a greate sample about this here. I expanded the sample to find a service by its contract.

[ServiceContract]
public interface IDiscoveryService
{
     [OperationContract]
     string[] GetOnlineServices(XmlQualifiedName criteria);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class DiscoveryService : DiscoveryProxy, IDiscoveryService
{
// DiscoveryProxy overides must come here
// GetOnlineServices implementation
public string[] GetOnlineServices(XmlQualifiedName criteria)
{
     List<string> result = new List<string>();
     List<EndpointDiscoveryMetadata> foundServices = _theLogic.FindInOnlineServices(criteria);
     foreach (EndpointDiscoveryMetadata foundService in foundServices)
     {
          result.Add(foundService.Address.ToString());
     }
     return result.ToArray();
}
}
// client code to find IPrint service addresses
IDiscoveryService discoveryProxy = new DiscoveryServiceProxy();
ContractDescription cd = ContractDescription.GetContract(typeof(IPrint));
XmlQualifiedName qn = new XmlQualifiedName(cd.Name, cd.Namespace);
string[] addresses = discoveryProxy.GetOnlineServices(qn);

About erictummers

My work as a recruited developer changes almost every month. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s