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);