Using WS-Discovery in WCF 4.0

Runtime endpoint discovery is one of the most challenging capabilities to implement in service oriented systems. Dynamically resolving service’s endpoints based on predefined criteria is a necessary functionality to interact with services which endpoint addresses change frequently. WS-Discovery is an OASIS Standard that defines a lightweight discovery mechanism for discovering services based on multicast messages. Essentially, WS-Discovery enables a service to send a Hello announcement message when it is initialized and a Bye message when is removed from the network. Clients can discover services by multicasting a Probe message to which a service can reply with a ProbeMatch message containing the information necessary to contact the service. Additionally, clients can find services that have changed endpoint by issuing a Resolve message to which respond with a ResolveMatch message.


Figure: WS-Discovery conceptual model

Contrary to other WS-* protocols, WS-Discovery has found a great adoption among the network device builders as it allows to streamline the interactions between these type of devices. For instance, a printer can use WS-Discovery to announce its presence on a network so that it can be discovered by the different applications that require printing documents. Windows Vista's contact location system is another example of a technology based on WS-Discovery.

The 4.0 release of Windows Communication Foundation includes an implementation of WS-Discovery that enables service’s endpoints as runtime discoverable artifacts. WCF enables the WS-Discovery capabilities in two fundamental models: Managed and Ad-Hoc. The managed mode assumes a centralized component called service proxy that serves as a persistent repository for all the services in a network. When a service is initialized it publishes its details to the discovery proxy so that it becomes accessible the the different clients in the network.

Contrary to the managed model, the Ad-Hoc mechanism does not rely on a centralized discovery proxy. In this model, services publish their presence in a network by multicasting announcement message that can be processed by the interested consumers. Additionally, clients can also multicast discover messages through the network in order to find a service that matches predefined criteria.

WCF's WS-Discovery managed mode will be the subject on a future post. Today we would like to illustrate the details of enabling dynamic discovery using the WS-Discovery 's Ad-Hoc model in WCF 4.0. This model is traditionally simpler to implement than the managed model although it can introduce some challenges from the management standpoint.

WCF 4.0 abstracts the WS-Discovery Ad-Hoc model using the ServiceDiscoveryBehavior which indicates that a service can be discoverable and the UdpDiscoveryEndpoint that instantiates a service endpoint that can listen for discovery requests. The remaining of this post will provide a practical example of the use of the WS-Discovery Ad-Hoc model in WCF 4.0

Let’s start with the following WCF service.

   1:      public class SampleService: ISampleService
   2:      {
   3:          public string Echo(string msg)
   4:          {
   5:              return msg;
   6:          }
   7:      }
   9:      [ServiceContract]
  10:      public interface ISampleService
  11:      {
  12:          [OperationContract]
  13:          string Echo(string msg);
  14:      }

Figure: Sample WCF Service

In order to make the service discoverable we first need to add the ServiceDiscoveryBehavior to the service behavior’s collection. As explained previously, this behavior indicates to the WCF runtime that the service supports the WS-Discovery protocol.

   1:  using (ServiceHost host = new ServiceHost(typeof(SampleService), new Uri(base uri...)))
   2:  {
   3:   ...
   4:    host.AddServiceEndpoint(typeof(ISampleService), new BasicHttpBinding(), String.Empty);
   5:    ServiceDiscoveryBehavior discoveryBehavior= new ServiceDiscoveryBehavior();             
   6:    host.Description.Behaviors.Add(discoveryBehavior);
   7:    ...       
   8:  }

Figure: Adding the service discovery behavior

The next step is to add the UdpDiscoveryEndpoint to the list of service endpoints so that our service can start listening for WS-Discovery messages.

   1:   host.AddServiceEndpoint(new UdpDiscoveryEndpoint());

Figure: Adding an UDP discovery endpoint

At this point our service is ready to receive and interpret WS-Discovery messages from the different clients on the network. However those clients are not yet aware of the existence of the service given that this one hasn’t published the Hello announcement message. We can accomplish this by simply adding a new UdpAnnoucement endpoint to the list of service endpoints.

   1:   discoveryBehavior.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());

Figure: Adding an UDP announcement endpoint

In order to dynamically discover services using the Ad-Hoc model, a WCF client instantiates a DiscoveryClient that uses discovery endpoint specifying where to send Probe or Resolve messages. The client then calls Find that specifies search criteria within a FindCriteria instance. If matching services are found, Find returns a collection of EndpointDiscoveryMetadata. The following code illustrates that concept.

   1:   DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
   2:   FindResponse discoveryResponse= discoveryClient.Find(new FindCriteria(typeof(ISampleService)));
   3:   EndpointAddress address = discoveryResponse.Endpoints[0].Address;
   5:  SampleServiceClient service = new SampleServiceClient(new BasicHttpBinding(), address);
   6:  service.Echo("WS-Discovery test");

Figure: WCF WS-Discovery client

The WCF implementation of the WS-Discovery Ad-Hoc model presents various aspects that I think are worth highlighting. First, WCF uses specialized discovery and announcement endpoints to process WS-Discovery messages isolating them from the service’s messages. Additionally, the use of service behaviors allow developers to incorporate the WS-Discovery capabilities as they are required without interfering with the normal service functioning. Finally, WCF clients can simply use the discovery client to dynamically resolve the service endpoint without having to make major modifications to its business logic.

We will cover the WS-Discovery managed mode in a future post.


  • Interesting- kind of like IoC for services... One comment: in my experience, all this WS-* stuff breaks down pretty quickly when you have to interop with non-.NET clients. I realize that MS can't exactly make Java/PHP/Ruby/etc play with WS-*, but it's annoying to have these "open" standards that get thrown out the window the second we have to talk to non-.NET systems. Invariably, even large SOA companies have told us "just use plain SOAP 1.1 over SSL".

  • I always problem with this Error "Target of Invocation" when i use wcf ws-Eventing(publish/Sub model), whenever client disconnected or busy to receive message from wcf service.

    Any idea to solve it?

  • It's really nice to figure out how extandable WCF architecture is. Just added Service Behavior to the server side.

  • Hi,

    Do you know some scenario where can I use this feature?

  • All the discovery examples I have seen assume basic HTTP as the binding.
    Is there any facility in discovery for also publishing the binding requirements to talk to the advertised endpoint?
    It seems to me that its a bit of a defecit if the client must 'assume' the protocol and security requirements, when it could be passed in the discovery meta-data.

  • Can WCF be used with the Compact Framework v3.5?


  • It was certainly interesting for me to read that blog. Thanx for it. I like such themes and everything connected to this matter. BTW, try to add some photos :).

  • Hello Sir,

    REF : WCF Discovery - Probe Match message.

    I would like to know how do I modify probe match message at run time.i have this requirement that i have to added element to the probe match before sending to the client,

    I have tried this using message inspector but it is not working during probe match message exchange the message null, not sure why this is happening

    you have pointer please let me know

    Thank You,
    Natraj Mustoori

  • I have problems with udp discovery from different subnet. Maybe the switch is rejecting udp packages. Any other ideas ???

  • -----------------------------------------------------------
    "That’s Too nice, when it comes in india desire it could make a Rocking spot for youngster.. desire that arrive correct."

  • -----------------------------------------------------------
    "I am speechless. This is often a fantastic webpage and incredibly partaking too. Outstanding function! That's not seriously significantly coming from an beginner author like me, but it is all I could feel immediately after enjoying your posts. Excellent grammar and vocabulary. Not like other weblogs. You really know what you are speaking about too. A lot that you just created me want to investigate much more. Your blog site has turn into a stepping stone for me, my friend. Thanks for your articulate quest. I truly loved the 27 posts that I have go through up to now. "

  • The catchy weblog with the exciting posts. You give the great data that a lot of individuals don't know prior to. most of one's contents are make me have much more knowledge. it truly is extremely distinct. I was impressed together with your web site. By no means be bored to take a look at your internet site once again. Have the good day.Retain enjoyed your blogging.

    Biology, Neuroscience

  • Yes there should realize the opportunity to RSS commentary, quite simply, CMS is another on the blog.

  • Get a hold of all which can be about African Mangoo within few seconds.

  • great article very usefull , thanks you

  • Wow, I have a website too but I can't write too as you do. Good stuff.

  • The that the next occasion I just read a blog, Hopefully
    that this doesnt disappoint me up to this. I am talking about, It was
    my path to read, but I really thought youd have something interesting to express.
    All I hear can be a number of whining about something you to
    could fix should you werent too busy seeking for attention.

  • you are in point of fact a good webmaster. The web site loading
    velocity is amazing. It seems that you're doing any distinctive trick. Furthermore, The contents are masterwork. you've performed a
    magnificent job in this topic!

  • Great ¡V I should certainly pronounce, impressed with your
    site. I had no trouble navigating through all tabs because well as related information ended
    up being truly straightforward to do to access.
    I recently found what I hoped for before you recognize
    it at all. Quite unusual. Is likely to appreciate it for those who add
    forums or anything, website theme . a tones tactic
    for your client to communicate. First-rate task.

  • Oh my goodness! a tremendous article dude. Thanks However I'm experiencing problem with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting identical rss downside? Anyone who is aware of kindly respond. Thnkx

  • Really a great blog! Just found it today and surely continue to look for updates.

  • there are so a whole lot of funny videos on that the internet to watch, i can
    laugh all day watching funny videos’

  • Very fine publish, thanks a ton for sharing. Do you happen to have
    an RSS feed I can subscribe to?

  • Youre so cool! I dont suppose Ive read something such as
    this before. So nice to look for somebody with
    authentic applying for grants this subject.
    realy i appreciate you for starting this up. this fabulous website have been
    some things that’s needed on that the web, somebody after some bit
    originality. helpful purpose of bringing new things to your web!

  • Add to that list Michael Pena and Bridget Moynahan, the
    two adults among three other youngsters whom Nantz and his men
    locate at the station.

  • Wow, wonderful blog layout! How long have you been blogging
    for? you make blogging look simple. That the overall look of your web site is fantastic,
    let alone the content!

  • First-rate day. i am doing research right now and your blog really helped me,

Comments have been disabled for this content.