Programming4us
         
 
 
Windows

SOA with .NET and Windows Azure : WCF Discovery (part 2) - Locating a Service Ad Hoc & Sending and Receiving Service Announcements

2/16/2011 5:53:58 PM

Locating a Service Ad Hoc

A service consumer can send out WS-Discovery probe messages to locate available services. The probe query can include compatibility criteria, such as a service contract or a service scope. Query parameters are encoded in a FindCriteria object.

There are two types of query criteria:

  • contract type names – searches for services with endpoints that implement the specified contract names

  • scope – searches for services with endpoints that match the specified scope (scopes are defined in the endpoint’s behavior and several matching options for complete or partial matches exist)

The DiscoveryClient class provided by WCF manages probes and raises FindProgressChangedEventArgs events when ProbeMatch responses come in:

discoveryClient =
new DiscoveryClient(new UdpDiscoveryEndpoint());
discoveryClient.FindProgressChanged +=
new EventHandler<FindProgressChangedEventArgs>
(OnFindProgressChanged);
discoveryClient.FindCompleted +=
new EventHandler<FindCompletedEventArgs>
(OnFindCompleted);
discoveryClient.FindAsync
(new FindCriteria(typeof(ICustomerService)),
discoveryClient);

Following this, we are submitting an asynchronous discovery query over UDP. ParseResult examines the service contract, scope, and metadata to determine if the responding service meets the requirements:

Example 4.
private void OnFindProgressChanged(
object sender, FindProgressChangedEventArgs e)
{
ParseResult(e.EndpointDiscoveryMetadata);
}

The DiscoveryClient class also implements a synchronous Find() method that takes a FindCriteria parameter that specifies query and query completion criteria, such as the number of responses or the time to wait for responses.

DiscoveryClient also exposes a Resolve() method to locate a replacement for a service that’s been previously available at a known address. Calling Resolve() follows the same pattern as Find().

Sending and Receiving Service Announcements

Services can announce their availability when they come online or go offline. These announcements can be received by all services listening for them. You configure a service to transmit announcements by adding announcementEndpoints to the serviceDiscovery behavior, as shown in Example 6.20. The service then issues announcements to the configured endpoint.

Example 5.
<behavior name="DiscoveryBehavior">
<serviceDiscovery>
<announcementEndpoints>
<endpoint name="udpEndpointName"
kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>

The standard udpAnnouncementEndpoint is preconfigured in WCF. Each announcement includes the service endpoint location and contract, as well as endpoint-specific metadata.

In highly dynamic environments, service consumers may want to track available services instead of probing for availability or relying on the discovery proxy. Probing introduces additional latency and therefore should not occur as part of the logic that resolves service locations. Frequent multicast probing further results in unnecessary network traffic. Instead, a consumer of dynamically available services can listen for announcement broadcasts and maintain its own list of services.

Services interested in receiving local UDP discovery announcements must open up a listener on the udpAnnouncementEndpoint endpoint. WCF provides a pre-built AnnouncementService class to handle service announcements. This class can raise the OnOnlineAnnouncement and OnOfflineAnnouncement events to the hosting application.

This example shows a listener configured for announcement services:

Example 6.
 <services>
... application service information ...

<service name="AnnouncementListener">
<endpoint kind="udpAnnouncementEndpoint" />
</service>
</services>

Next, we register events with AnnouncementService:

Example 7.
AnnouncementService announcementService =
new AnnouncementService();
announcementService.OnlineAnnouncementReceived +=
new EventHandler<AnnouncementEventArgs>
(this.OnOnlineAnnouncement);
announcementService.OfflineAnnouncementReceived +=
new EventHandler<AnnouncementEventArgs>
(this.OnOfflineAnnouncement);

The events receive an EndpointDiscoveryMetadata object, just like the response to a discovery probe. A FindCriteria object determines if the metadata matches endpoint requirements. In the following example, we query an EndpointDiscoveryMetadata announcement for compatibility with the ICustomerService contract:

Example 8.
private void OnlineAnnouncement
(object sender, AnnouncementEventArgs e)
{
EndpointDiscoveryMetadata metadata =
e.EndpointDiscoveryMetadata;
FindCriteria criteria =
new FindCriteria(typeof(ICustomerService));
if (criteria.IsMatch(metadata))
{
... further examine endpoint metadata ...
... store endpoint address for service access ...
}
Other -----------------
- Windows 7 : Working with Network Files Offline (part 3) - Prohibiting a Network Folder from Being Made Available Offline & Encrypting Offline Files
- Windows 7 : Working with Network Files Offline (part 2) - Changing the Amount of Disk Space Used by Offline Files
- Windows 7 : Working with Network Files Offline (part 1) - Activating the Offline Files Feature & Making a File or Folder Available for Offline Use
- Windows 7 : Sharing Resources with the Network (part 2) - Monitoring Your Shared Resources
- Windows 7 : Sharing Resources with the Network (part 1) - Setting Sharing Options & Creating User Accounts for Sharing
- Windows 7 : Accessing a Shared Printer
- Windows 7 : Creating a Network Location for a Remote Folder
- Windows Vista: Windows Reliability and Performance Monitor and Task Manager
- Windows Vista: Configuring Internet Explorer 7.0 - Dynamic Security and Protected Mode
- Windows 7: Mapping a Network Folder to a Local Drive Letter
- SOA with .NET and Windows Azure: WCF Extensions - WCF Router (part 2) - Routing Configuration
- SOA with .NET and Windows Azure: WCF Extensions - WCF Router (part 1) - The RoutingService Class & Routing Contracts
- Windows 7: Accessing Shared Network Resources
- Windows 7: Managing Wireless Network Connections (part 4) - Creating User-Specific Wireless Connections
- Windows 7: Managing Wireless Network Connections (part 3) - Reordering Wireless Connections
- Windows 7: Managing Wireless Network Connections (part 2) - Working with Wireless Connection Properties
- Windows 7: Managing Wireless Network Connections (part 1) - Creating an Ad Hoc Wireless Network
- Windows7: Managing Network Connections (part 5) - Using a Network Connection to Wake Up a Sleeping Computer
- Windows7: Managing Network Connections (part 4) - Finding a Connection’s MAC Address
- Windows7: Managing Network Connections (part 3) - Setting Up a Static IP Address
 
 
Most View
- Microsoft XNA Game Studio 3.0 : Adding Bread to Your Game (part 2) - Improving Programs Using Methods, Handling Collisions
- Overview of Process Management in Microsoft Visio 2010 (part 3) - Validation of process diagrams
- Windows Phone 7: Posting to Facebook or Windows Live
- Mobile Handheld Devices : DATA SYNCHRONIZATION
- Exchange Server 2010 : Deploying Unified Messaging (part 1)
- Certificate-Based Encryption
- CSS for Mobile Browsers : CSS Sprites
- Programming Windows Phone 7 : An XNA Program for the Phone (part 1)
- DirectX 10 Game Programming : Shaders and Effects - High Level Shading Language
- Windows 7: Troubleshooting Device Problems (part 2) - Displaying a List of Nonworking Devices
Top 10
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 3) - Configuring Recipient Filtering
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 2)
- Implementing Edge Services for an Exchange Server 2007 Environment : Utilizing the Basic Sender and Recipient Connection Filters (part 1)
- Implementing Edge Services for an Exchange Server 2007 Environment : Installing and Configuring the Edge Transport Server Components
- What's New in SharePoint 2013 (part 7) - BCS
- What's New in SharePoint 2013 (part 6) - SEARCH
- What's New in SharePoint 2013 (part 6) - WEB CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 5) - ENTERPRISE CONTENT MANAGEMENT
- What's New in SharePoint 2013 (part 4) - WORKFLOWS
- What's New in SharePoint 2013 (part 3) - REMOTE EVENTS