Thursday, April 15, 2010

WCF 4.0 Routing Service –Content Based Routing (CBR).

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.

EmployeeTimeSheetSvc [Hosting]


      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]


ContractorTimeSheetSvc [Hosting]


  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.]



So using WCF 4.0 Routing service it is very easy to configure Router service to forward a message on to another WCF service based on the content of the received message.




Anonymous said...


greenestoffice said...

Nice sites.


Harinatha Narisetty said...

Nice one.. Keep posting

Shravya said...

It is great to see you explained WCF advanced topics in simple way.

I would suggest to improve on the graphics you provided. Graphics look too stretched and some code are not visible.
Use SnagIt!!!

All the best but keep posting!

Manjunath B.E said...

Thanks a lot dude....

HARSHA said...

can anyone give me source code for these projects.Half of the code is missing in img and i m not able to complate the eg though its good one.

Bell Brown said...

systems make employee time monitoring simple and they consolidate the information ready for management to look at and keep a record of.