Introduction to Routing Service in WCF 4.0


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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 Service would be useful.

1.Consider 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.

2.Say 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.

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

4.To 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.

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

2.ISimplexSessionRouter: It is one way but works on a session.

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

4.IDuplexSessionRouter: 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.





5.Even 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.

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

2.In 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" %>

3.In 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

1.Add routing service endpoints.

2.Add the routing service behavior along with a filter table name.

3.Add client service endpoints

4.Add the MessageFilters with filterType and filterData

5.Link 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!

About the Author

V.N.S Arun

I work for an MNC in Bangalore, India. I am fond of writing articles, posting answers in forums and submitting tips in dotnet. To contact me please feel free to make use of the "Send Email" option next to the display name.

Related Articles


  • Steps to Consume WCF routing Service

    Posted by Arun Tripathy on 03/27/2015 05:26am

    Hi Arun.In case of normal way of WCF service consumption , at client side, Service Contract, Data Contract etc are available. But in case of WCF routing service how to interact with service contract , DataContracts etc through the in-built IRequestReplyRouter contract of Router Service.

  • Smart Device to WCF Router connection not working

    Posted by Leela Kumar on 03/12/2013 04:43am

    Hi , Am working in C# smart device (Mobile Apps).this mobile apps connecting to Wcf Router and when i am pass request to Wcf Router ,Its throw Error Message to "Not establish connection to network..."

  • Very nice..

    Posted by Malte Baden Hansen on 12/19/2012 08:58pm

    Very nice article. I like your keep it simple approach, many tutorials only add a single client to the routing config. Which seems a bit pointless unless its to bridge different protocols. I do however really miss an explaination to what exactly that configuration does when the wcf router is running.

  • Need more information

    Posted by SUJITH on 06/07/2012 05:38pm

    you are not specified how to access routing service on the client please include that also. Otherwise a user new wcf does not understand anything from this. He will stuck up with adding this on the the routing service. Please add the following details also 1. WCF Services that are configured in the section. 2.How to consume these through routing services

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date