Adding Error Handling to content based routing with WCF

Yesterday I blogged about content based routing with WCF. Today I want to talk about error handling. What if you route to a service that is not available. WCF allows you to define backup services using a backup list.

So let’s do this: start by building a compatible backup service. In this case I will be using named pipes because this is a very reliable transport:

static Uri ServiceUri = new Uri("net.pipe://localhost/BackupProductService");

static void Main(string[] args)
  using (ServiceHost host = new ServiceHost(typeof(BackupProductsService), ServiceUri))

    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("Backup product service is running");


Now let’s create the backup list in the routing service’s configuration:


    <backupList name="backup">
      <add endpointName="backup" />


This adds a backup endpoint called “backup” which we of course also need to add the the client endpoints:

  <endpoint address="net.tcp://localhost:8010/ProductService1"
            binding="netTcpBinding" contract="*" name="toys" />
  <endpoint address="net.tcp://localhost:8011/ProductService2"
            binding="netTcpBinding" contract="*" name="beverages" />
  <endpoint address="net.pipe://localhost/BackupProductService"
            binding="netNamedPipeBinding" contract="*" name="backup" />


Finally we add the backup list to the filterTable:

  <filterTable name="productRouting">
     <add filterName="filterToys" endpointName="toys" backupList="backup" />
     <add filterName="noToys" endpointName="beverages" backupList="backup" />


If we now run the sample, but without the toys or beverages service, routing will automatically divert to the backup service.