New Features of WCF 4.0: Part II

Previous Article
 
 

Introduction

 
Microsoft.NET 4.0 and Visual Studio.NET 2010 ships a lot of new features in their underlying technologies. In this series of articles, I want to talk about the new features in the area of Windows Communication Foundation (WCF) in order to improve the development experience, enable more communication scenario, support new WS-* standards and provide a good integration with Windows Workflow Foundation (WF).
 
The new features are essentially the following: simplified configuration, standard endpoints, IIS hosting without a SVC file, support for WS-Discovery, routing service, REST improvements, enhancements for the integration with WF to support workflow services, simple byte stream encoding and ETW tracing.
 
In this series of article, I will illustrate each feature explaining the principles and showing some examples.
 

Support for WS-Discovery

 
This is the major feature that WCF 4.0 ships. The context of the scenario covered by this features is an organization whose business approach is service-oriented and the services are constantly joining and leaving the network. The problem in this context is that the runtime location of these services is constantly changing and the clients need to dynamically discover the new locations.
 
In order to cover this common scenario, the WS-Discovery specification was defined to extend the SOAP protocol for dynamically discovery of services location. The approach enables a client to probe for service endpoints matching certain criteria to get a list of candidates. Then this client can choose one service from the list of candidates.
 
WS-Discovery defines two modes of operation:  managed and ad-hoc mode.
 
In the managed mode there is a central server called discovery proxy that services used to publish their underlying endpoints. Then, clients talk directly to the discovery proxy to locate services based on different criteria. WCF 4.0 provides classes to implement your own discovery proxy.
 
In the ad-hoc mode, there is no central server. Service announcement and client requests are sent in a multicast fashion. Clients discover the services by sending multicast messages, then services that match the probe respond directly to the client. In order to minimize the need of the client discovering new services, then the client can listen to the network for new service announcements. The limitation of this operation mode is that the service discovery operation is limited by the protocol used for multicast messages. In the case of using UDP protocol, the multicast is limited to the local network.
 
Let's add support for WS-Discovery to our EchoService service defined in the first part of the series.
 
The service contract is shown in the Listing 1
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. namespace WCF_NewFeatures  
  8. {  
  9.     [ServiceContract]  
  10.     public interface IEchoService  
  11.     {  
  12.         [OperationContract]  
  13.         string Echo(String message);  
  14.     }  
  15. }  
Listing 1
 
And the service implementation is shown in the Listing 2.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7. namespace WCF_NewFeatures  
  8. {  
  9.     public class EchoService : IEchoService  
  10.     {  
  11.         public string Echo(String message)  
  12.         {  
  13.             return "Called the Echo Service with message " + message;  
  14.         }  
  15.     }  
  16. }  
Listing 2
 
Now let's add WS-Discovery in the configuration file (see highlighted in yellow in the Listing 3).
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.           <system.serviceModel>  
  4.                    <services>  
  5.                              <service name="WCF_NewFeatures.EchoService" behaviorConfiguration="WCF_NewFeatures.EchoServiceBehavior">  
  6.                                       <endpoint address="" binding="wsHttpBinding" contract="WCF_NewFeatures.IEchoService">  
  7.                                       </endpoint>  
  8.                                       <endpoint name ="udpDiscovery" kind ="udpDiscoveryEndpoint" />  
  9.                                       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>  
  10.                              </service>  
  11.                    </services>  
  12.                    <behaviors>  
  13.                              <serviceBehaviors>  
  14.                                       <behavior name="WCF_NewFeatures.EchoServiceBehavior">  
  15.                                                 <serviceMetadata httpGetEnabled="true"/>  
  16.                                                 <serviceDebug includeExceptionDetailInFaults="false"/>  
  17.                                                 <serviceDiscovery />  
  18.                                       </behavior>  
  19.                              </serviceBehaviors>  
  20.                    </behaviors>  
  21.           </system.serviceModel>  
  22. </configuration>  
Listing 3
 
And the service host is defined as shown in the
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Description;  
  7. namespace WCF_NewFeatures  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             ServiceHost serviceHost = new ServiceHost(typeof(WCF_NewFeatures.EchoService),new Uri("http://localhost:8080/Services/EchoService"));  
  14.             serviceHost.Open();  
  15.             System.Console.WriteLine("The EchoService has started");  
  16.             foreach (ServiceEndpoint se in serviceHost.Description.Endpoints)  
  17.             {  
  18.                 System.Console.WriteLine("Address:{0}, Binding:{1}, Contract:{2}", se.Address, se.Binding.Name, se.Contract.Name);  
  19.             }  
  20.             System.Console.WriteLine("Please, press any key to finish ...");  
  21.             System.Console.ReadLine();  
  22.             serviceHost.Close();  
  23.         }  
  24.     }  
  25. }  
Listing 4
 
When you run the application, you can see the WSDL at http://localhost:8080/Services/EchoService as shown in the Figure 1.
 
1.gif 
Figure 1
 
Next step is to consume this service. Let's add a new console project as the client-side and add a reference to the System.ServiceModel.dll and System.ServiceModel.Discovery.dll assemblies (see Figure 2).
 
2.gif 
Figure 2
 
Next step is to move to the project directory and run the command svcutil.exe http://localhost:8080/Services/EchoService?wsdl to generate the service proxy and the underlying configuration. Let's include only the service proxy definition because the service location (in the configuration file) will be discovered dynamically.
 
Now let's write the discovery code for the client application (see Listing 5).
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Discovery;  
  7. namespace WCFClientConsApp  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());  
  14.             FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(IEchoService)));  
  15.             EndpointAddress endpointAddress = findResponse.Endpoints[0].Address;  
  16.             EchoServiceClient echoServiceClient = new EchoServiceClient(new WSHttpBinding(), endpointAddress);  
  17.             string strEcho = echoServiceClient.Echo("Hello world!");  
  18.             System.Console.WriteLine("The service result is "+strEcho);  
  19.             System.Console.WriteLine("Please, press any key to finish ...");  
  20.             System.Console.ReadLine();  
  21.         }  
  22.     }  
  23. }  
Listing 5
 
After you run the client solution and wait for a while, you will receive the output shown in the Figure 3.
 
3.gif 
Figure 3
 

Conclusion

 
In this series of article, I've explained the new features of WCF 4.0 through concepts and examples.
 
Next Articles 
 


Similar Articles