Introduction to Routing Service in WCF 4.0

Routing Service feature in WCF 4.0 is
introduced by Microsoft to help developers in building WCF services with ease.
The feature can route the message requests to other configured client WCF
services based on the message content in the incoming client request. The
routing principle is said to be Content Based Routing.

Need for a Routing Service

There has long been a need for this feature to be included
in WCF 4.0. Below are some scenarios where WCF Routing
would be useful.

that multiple WCF services are hosted separately with their own end points, and
based on the incoming SOAP message request from a client, routing should be
done to the appropriate WCF service.

the client only communicates through HTTP binding and the WCF service processes
only NET TCP bindings. In this case the router takes care of bridging the
client and service operating on different protocols.

act as a load balancer for the same WCF service deployed on different hosts.

implement the fail over mechanism for a WCF service.

In-built Contracts

In order to implement the WCF
Routing Service .NET framework
4.0 offers a few in-build contracts. These are placed under the namespace
System.ServiceModel.Routing. The Routing Service can be configured with any of
the contracts listed below. Based on these contracts the WCF communication
channels are established.

It is a one way model for which session is not mandatory. Ideal for performing
tasks like logging, message queuing, etc.

It is one way but works on a session.

It is the usual client message request and service response model.

It works over session and supports performing callbacks to the client

Message Filters

Message Filters are used by the
WCF routing service to consider which part of the message has to be matched in
order to perform the routing to the respective service. Below are some of the
available message filters.





a Custom one can be created

Sample Code

In this section I will provide
sample code for implementing WCF Routing Service. The routing service can be
completely configured through the configuration file or through code behind. In
my sample code I have chosen to use the configuration method.

a WCF service project, delete all the .cs files and add the reference to the
library System.ServiceModel.Routing.

the .svc file add the below entry.

<%@ ServiceHost Language="C#" Debug="true" Service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

the Web.Config add the below entries to configure the routing service.

              <service name="DemoRoutingService" behaviorConfiguration="MyRoutingServiceBehavior">
                                  <add baseAddress="http://localhost:5555/RoutingService/DemoRouter"/>
                     <endpoint name="RequestReplyBindingEP" address="http://localhost:5555/RoutingService/DemoRouter" binding="wsHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter"></endpoint>
                     <behavior name="MyRoutingServiceBehavior">
                           <serviceMetadata httpsGetEnabled="True"/>
                           <routing filterTableName="routingFilterTable"/>
              <endpoint name="GreetingService" address="http://localhost:6666/GreetingService/Greeting" binding="wsHttpBinding" contract="IGreetingService"></endpoint>
              <endpoint name="CalculatorService" address="net.tcp://localhost:6666/CalculatorService/Calculator" binding="netTcpBinding" contract="ICalculatorService"></endpoint>
                     <filter name="EPFilter" filterType="EndpointName" filterData="RequestReplyBindingEP"/>
                     <filter name="EPAddressFilter" filterType="EndpointAddress" filterData="http://localhost:5555/RoutingService/DemoRouter"/>
                     <filterTable name="routingFilterTable">
                           <add filterName="EPFilter" endpointName="GreetingService"/>
                           <add filterName="EPAddressFilter" endpointName="CalculatorService"/>
   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

Below is the sequence for the routing service configuration

routing service endpoints.

the routing service behavior along with a filter table name.

client service endpoints

the MessageFilters with filterType and filterData

the message filter with respective client endpoints through filterTable.

There is also an interesting concept
called back up endpoints. The messages are routed to these back up endpoints
when the main client WCF service endpoint routing fails for some reason. There
can be multiple backup endpoints specified and the failover will happen in the
provided order.

       <backupList name="GreetingBackupEPList">
              <add endpointName="BackupGreetingService1"/>
              <add endpointName="BackupGreetingService2"/>
              <add endpointName="BackupGreetingService3"/>

This back up list can be linked
to the filterTable.

I hope this article provided a
good step-in for the WCF 4.0 Routing Service feature. There is still much more
to explore and experiment.

Happy reading!

More by Author

Must Read