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))
{
host.Open();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Backup product service is running");
Console.ReadKey();
}
}
Now let’s create the backup list in the routing service’s configuration:
<routing>
<backupLists>
<backupList name="backup">
<add endpointName="backup" />
</backupList>
</backupLists>
This adds a backup endpoint called “backup” which we of course also need to add the the client endpoints:
<client>
<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" />
</client>
Finally we add the backup list to the filterTable:
<filterTables>
<filterTable name="productRouting">
<add filterName="filterToys" endpointName="toys" backupList="backup" />
<add filterName="noToys" endpointName="beverages" backupList="backup" />
</filterTable>
</filterTables>
If we now run the sample, but without the toys or beverages service, routing will automatically divert to the backup service.