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

image

now let’s create each service component.

Timesheet Service contract definition

 image

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]

 image

      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]

image

ContractorTimeSheetSvc [Hosting]

      image

  ContractTimeSheet service listening  at the following http://localhost:8001/ContractorService" endpoint with basicHttpBinding.


image

 

Router Service [Hosting]

First we need to add the following two .NET Assembly to the Routing Service project.

image

     Here we are hosting RoutingService class.

image

       Here the  RoutingService implements the System.ServiceModel.Routing.IRequestReplyRouter contract. There are other contracts that you could use  based your service requirements .

image

image

 

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

image

image

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.

Nandri(Thanks)

SreenivasaRagavan

6 comments:

Web Timesheet said...

Nice sites.

---------------------------------


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

Unknown said...

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