Creating a WCF 4.0 Routing Service

By | August 18, 2011

One of the new WCF 4.0 features is routing. Routing can be used for multiple purposes:

– Service versioning
– “Extended” load balacing
– Content-based routing scenario
– Service partitioning
– Protocol bridging
– etc.

In this example, I have two person services. One in London and one in Paris. The client knows only the WCF router.
The router filters the client request and calls the appropriate person service.
London uses http and Paris uses net.tcp binding. The clients and the router are communicating via http.
WCF router

The contract for the both person services contains one method:

 
[ServiceContract]
public interface IPersonService
{
    [OperationContract]
    Person GetPerson(PersonFilter filter);
}

Implementations (London/Paris):


// London service
public class PersonService : IPersonService
{
    public Person GetPerson(PersonFilter filter)
    {
        if (filter.Location.ToLower() == "london")
            return new Person() {  Firstname = "John", Lastname = "Owen" };
        return null;
    }
}

// Paris service
public class PersonService : IPersonService
{
    public Person GetPerson(PersonFilter filter)
    {
        if (filter.Location.ToLower() == "paris")
            return new Person() { Firstname = "André", Lastname = "Moutiers" };
        return null;
    }
}

The London service uses basicHttpBinding. Here is the configuration.


  
    
      
        
        
          
            
          
        
      
    
  

And the Paris service uses netTcpBinding:



  
    
      
        
        
          
            
          
        
      
    
  

The services are running in console applications. (self-hosted)

// London
class Program
{
    static void Main(string[] args)
    {
        var host = new ServiceHost(typeof(PersonService.PersonService));
        host.Open();
        Console.WriteLine("London PersonService running.");
        Console.ReadLine();
        host.Close();
    }
}

// And paris
class Program
{
    static void Main(string[] args)
    {
        var host = new ServiceHost(typeof(PersonService.PersonService));
        host.Open();
        Console.WriteLine("Paris PersonService running.");
        Console.ReadLine();
        host.Close();
    }
}

The WCF router must know the location of these services. And by filtering the client request, the router has to
decide which service has to be called. The most things are done in the config file. Read the XML comments.


  
    
      
      
        
          
          
          
        
      
    
    
      
      
        
        
        
      
      
        
        
          
          
        
      
    
    
      
      
        
        
          
            
          
        
      
    
    
    
      
      
    
  

WCF router hosting. The “RoutingService” class is a .NET 4.0 class.

class Program
{
    static void Main(string[] args)
    {
        var host = new ServiceHost(typeof(RoutingService));
        host.Open();
        Console.WriteLine("Routing running.");
        Console.ReadLine();
        host.Close();
    }
}

We have now three services:

Name Function Address
WCF router Routing http://localhost:9000/PersonServiceRouter
London service Data service http://localhost:8000/Personservice
Paris service Data service net.tcp://localhost:8500/Personservice

The client has just to know the WCF router.
This code here, calls the service method “GetPerson” with a filter.
The Property “PersonFilter.Location” is important for the WCF router. The configurated filter (see WCF router config)
checks the value of the “Location” and calls the appropriate service (London or Paris).

class Program
{
    static void Main(string[] args)
    {
        // create proxy object
        var binding = new BasicHttpBinding();
        var endpoint = new EndpointAddress("http://localhost:9000/PersonServiceRouter");
        var proxy = ChannelFactory.CreateChannel(binding, endpoint);
         
        // Call WCF router with param "london". -> London PersonService is called.
        CallService(proxy, "london");
        // Call WCF router with param "paris". -> Paris PersonService is called.
        CallService(proxy, "paris");

        Console.ReadLine();
    }

    static void CallService(IPersonService proxy, string location)
    {
        Console.WriteLine("-------------------------");
        Console.WriteLine("Service URL: http://localhost:9000/PersonServiceRouter");
        Console.WriteLine("Filter: " + location);

        // Set the person filter
        var filter = new PersonFilter() { Location = location };
        var person = proxy.GetPerson(filter);

        Console.WriteLine("Firstname: {0}, Lastname: {1}", person.Firstname, person.Lastname);
        Console.WriteLine("-------------------------");
        Console.WriteLine(string.Empty);
    }
}

This is it.
WCF router example

And here is a printscreen of my VS.NET solution:
WCF router solution

EDIT: Here you can download the solution: WcfRouting