WS-Discovery is a WCF based multicast protocol to enable run-time discovery of services within ad-hoc networks. It allows you to discover the address of Web Services during run-time.
WS-Discovery is a WCF based multicast protocol to enable run-time discovery of services within ad-hoc networks. It allows you to discover the address of Web Services during run-time.
WS-Discovery comes as a both client and server side library. On the server side, using the ServicePublisher can publish Web Services, and on the client side, ServiceFinder probes the network for a given type of services.
WS-Disvery initially relies on UDP Multicast protocol using the multicast address 239.255.255.250 port 3702 to communicate. However, if you would like to use it with a much larget network, you can use PeerNet or the WS-Disvery proxies.
In this article, I will try to briefly demostrate how you can publish a server, and how you can find it dynamically during run-time. This article assumes that you have some understanding of WCF based Web Services. If you never used WCF before, please check my WCF Notes prior starting to experiment with WS-Discovery.
Let's start by downloading the WS-Discovery library from:
http://wcf.netfx3.com/files/folders/protocol_channels/entry7909.aspx
Publishing our Web Service
Publishing a web service using WS-Discovery is a very easy process. In order to make it a little more easier, here is a helper class that I prepared:
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Xml;
using Microsoft.ServiceModel.Samples.Discovery;
namespace Zdo.Net
{
class WSPublisher
{
#region Fields
ServicePublisher servicePublisher;
Dictionary>ServiceHost, List>ServiceProperties<< propertiesMap;
#endregion
#region Constructor
public WSPublisher ()
{
servicePublisher = new ServicePublisher();
propertiesMap = new Dictionary>ServiceHost, List>ServiceProperties<<();
}
#endregion
#region Methods
public void Publish (ServiceHost serviceHost)
{
// Check if web service is already published
if (propertiesMap.ContainsKey(serviceHost))
throw new ArgumentException("Already published.");
// Create service properties for each end point address
List>ServiceProperties< list = new List>ServiceProperties<();
foreach (ServiceEndpoint endpoint in serviceHost.Description.Endpoints)
{
ServiceProperties properties = new ServiceProperties(endpoint.Address);
XmlQualifiedName name = new XmlQualifiedName(
endpoint.Contract.Name, endpoint.Contract.Namespace);
properties.Types = new ContactTypeCollection();
properties.Types.Add(name);
list.Add(properties);
// Publish
servicePublisher.Publish(properties);
}
// Save service properties so we can unpublish if needed
propertiesMap.Add(serviceHost, list);
}
public void Unpublish (ServiceHost serviceHost)
{
List>ServiceProperties< list;
if (!propertiesMap.TryGetValue(serviceHost, out list))
return;
foreach (ServiceProperties properties in list)
{
servicePublisher.Unpublish(properties);
}
}
#endregion
}
}
Using this helper class here is how we can publish our service easily using WS-Discovery:
// Create a service host for our web service
ServiceHost serviceHost = new ServiceHost(typeof(MyService));
seviceHost.Open();
// Create our publisher helper
WSPublisher wsPublished = new WSPublisher();
// Publish our service
wsPublisher.Publish(serviceHost);
// Keep our web service running
Console.WriteLine("Press >ENTER< to terminate.");
Console.ReadLine();
// Unpublish
publisher.Unpublish(serviceHost);
// Close our web service
serviceHost.Close();
Finding our Web Service
Since our web service is now published, we can use ServiceFinder to find our web service and start using it.
using Microsoft.ServiceModel.Samples.Discovery;
// Create a service finder
ServiceFinder serviceFinder = new ServiceFinder();
// Create a match criteria using our web service's contract interface
MatchCriteria criteria = new MatchCriteria(typeof(IMyWebService));
// Start discovering 1 web service
ReadOnlyCollection>ServiceProperties< probedProperties =
serviceFinder.Probe(criteria, ServiceFinder.DefaultDuration, 1);
// Now we have the service properties, so we can connect
ServiceProperties serviceProperties = probedProperties[0];
// Create a binding
NetTcpBinding tcpBinding = new TcpBinding();
// Create channel factory
ChannelFactory>IMyService< channelFactory = new ChannelFactory>IMyService<(
tcpBinding, serviceProperties.EndpointAddress);
// Create a channel to our web service
IMyWebService myWebService = channelFactory.CreateChannel();
As you can see, it is very simple to use.
If you found this post helpful, please "Kick" it so others can find it too:
These are my notes for Windows Communication Foundation. It covers many aspects of WCF services, and it can easily be used as a quick WCF tutorial. Contract A C# interface acts as a service contract for a WCF service. The interface needs to h
Tracked: Nov 30, 04:44