What is Content Based Routing?
Content-Based Routing seeks to route messages. Messages are routed not by a specified destination, but by the actual content of the message itself. In a typical application, a message is routed by being opened and then having a set of rules applied to its content.
In this blog post I am going to show how to configure WCF 4.0 Routing Service as Content based router. Here I am going to take an example scenario is Worker Timesheet submission. The timesheets are submitted to one of two services, once service is responsible for processing employees only timesheets and other service will process Contractors Timesheet. The reason two different services are required is that employee timesheets are automatically processed and approved but contractors timesheets require human interaction to validate the timesheets before their approval.
Another thing is that we do not want the client to know that there are two separate services to handle this timesheets and further to this we do not want them to be responsible for picking which service to call and send the timesheet to.
Here is my VS.NET project structure to implement this above scenario in WCF 4.0 using Routing Service.
now let’s create each service component.
Timesheet Service contract definition
Now we need to implement the above contract in two different services EmployeeTimeSheetSvc and ContractorTimeSheetSvc. for the demo purpose we simply write out the Timesheet details to the console window.
EmployeeTimeSheet service listening at the following http://localhost:8000/EmployeeService" endpoint with basicHttpBinding. here we are hosting EmployeeTimeSheetSvc service in Windows console application [ for real world we should be hosting it in IIS /WAS]
ContractTimeSheet service listening at the following http://localhost:8001/ContractorService" endpoint with basicHttpBinding.
Router Service [Hosting]
First we need to add the following two .NET Assembly to the Routing Service project.
Here we are hosting RoutingService class.
Here the RoutingService implements the System.ServiceModel.Routing.IRequestReplyRouter contract. There are other contracts that you could use based your service requirements .
If you look at the above configuration the routing section specifies two filters which uses XPath to perform the routing. These filters are used in the filterable section which specifies that when the filter’s condition are matched which endpoint should be called. In this example we are checking if the employee type is PERM then the “empEP” endpoint is called and if the employee type is CONTRACTOR then “conEP” endpoint is called.
WCF Client Application
Here we are invoking Timesheet service using ChannelFactory class. This allows us to call WCF service without adding a Service reference[using VE.NET IDE] . but we need to Reference the Service Contract interface though [ We can simply copy the service contract Interface definition to this project.]