Friday, June 11, 2010

WCF 4.0 Routing Service

Routing service is one of the very important and coolest features in WCF 4.0. In WCF 3.5 if you would like to implement Routing service, you need to explicitly code for that. Routing service allows you to define a single service for your client and it automatically takes care of routing the request to respective underlying services based on the routes and filters configured.

Routing Service can be used in multiple scenarios; here are some of the very frequent ones

1) Scenario 1: When you do not want to expose the internal logic or functionality to the outside world, just expose the Routing service and keep everything else behind Routing Service.

2) Scenario 2: When you want to load balance your services or when you want to redirect a particular type of service request to a particular service but still keeping the same interface for the client. Routing service will inspect the request for you based on the filters and redirect it to the corresponding service. This is know is Content based routing

3) Scenario 3: Failover implementation, Routing services allows you to define backup service, when ever your main service is not available request will be automatically transferred to backup service.

4) Scenario 4: There might be scenario when you want to expose service on a different protocol to the outside world but internally using a different protocol. This is know as Protocol bridging as routing service is acting as bridge between two protocols

System.ServiceModel.Routing is the root namespace for all the routing service related functionalities.

Source code for content based routing service is attached. It contains the following items

1) Two normal WCF services : Air service ( Air.svc) and Road Service (RoadService.svc)

2) One Routing Service : RoutingService.svc

3) Console Client: This call the routing service.

To test this application first run all the three services by right clicking and selecting view in browser for each of these files i.e Air.svc, RoadService.svc and RoutingService.svc. Now run the client console application which sends two requests to routingservice and routing service internally based on filters (as shown below),routes one request to air service and second to road service which can be verified by looking at the output in console window. Below is the section of routing section from configuration file, in you see there are two filters defined AirFilter and RoadFilter. Based on these filters and filterdata routing service decides where to route the request

Download full Source Code

No comments: