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

1.Action

2.EndpointName

3.EndpointAddress

4.XPath

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=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

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

<system.serviceModel>
       <services>
              <service name="DemoRoutingService" behaviorConfiguration="MyRoutingServiceBehavior">
                     <host>
                           <baseAddresses>
                                  <add baseAddress="http://localhost:5555/RoutingService/DemoRouter"/>
                           </baseAddresses>
                     </host>
                     <endpoint name="RequestReplyBindingEP" address="http://localhost:5555/RoutingService/DemoRouter" binding="wsHttpBinding" contract="System.ServiceModel.Routing.IRequestReplyRouter"></endpoint>
              </service>
       </services>
       <behaviors>
              <serviceBehaviors>
                     <behavior name="MyRoutingServiceBehavior">
                           <serviceMetadata httpsGetEnabled="True"/>
                           <routing filterTableName="routingFilterTable"/>
                     </behavior>
              </serviceBehaviors>
       </behaviors>
       <client>
              <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>
       </client>
       <routing>
              <filters>
                     <filter name="EPFilter" filterType="EndpointName" filterData="RequestReplyBindingEP"/>
                     <filter name="EPAddressFilter" filterType="EndpointAddress" filterData="http://localhost:5555/RoutingService/DemoRouter"/>
              </filters>
              <filterTables>
                     <filterTable name="routingFilterTable">
                           <add filterName="EPFilter" endpointName="GreetingService"/>
                           <add filterName="EPAddressFilter" endpointName="CalculatorService"/>
                     </filterTable>
              </filterTables>
       </routing>
   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

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.

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

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

Comments

  • Fake Oakley Radar free shipping

    Posted by fmeapjqtn on 07/04/2013 10:41pm

    FakE OaklEyS ,Oakley fashion sport sunglasses. Led the international status like a sports brand, as well as a beautiful fashion appearance of sunglasses. The sports sunglasses, sunglasses to help promote explore the amount with the market, also are developed using shatterproof degree lens, in addition to the face according to the tailor-made cheap Oakley sunglasses. For style forward even Oakley at the pinnacle. Oakley use of the latest model generated glasses. Oakley shade in the application can be quite a large celebrity list. FAke OAkley Big TAco ,The sale of Oakley sunglasses will not be simply a tool to prevent sunshine, and also literally role on the decorative surface. Fresh dawn in the shadow with the sun, enjoy some of Dennis to offer an answer to acquire virtually any obsolete temporary workers, you will find outside play Oakley jaw, to be able to have a strange John Coast Recreation sunshade. Premium quality and technology experts, each design is obvious this is a perfect brand, if you like a good and effortlessly cool finish in your look. Another style versus the other necessities, improve support services needs, shops want to offer the important modifications in their future Oakley sun tone. cheap oakley sunglasses ,Oakley sports sunglasses better sunglasses sold to discover. You'll find a framework and coated lenses to adjust to every face type. Date night, a copy of jewelry function to get a wide range of the style industry praise, Sales and affordable because they are designed for just a great alternative to platinum, gold, and also other jewelry Oakley sunglasses. fake ray ban sunglasses ,Note are unable to see ultraviolet illumination, the approval Oakley sun screen lotion, to lose the sun's ultraviolet radiation power, in particular, is a bit more harmful ultraviolet illumination, wil attract. A really professional sports mirror using a unique personality, the full selection of functionality to win the respect of various celebrities around the world. longchamp le pliage ,Bending can change the symptoms contours with the lenses, causing optical distortion, therefore you still find the optimized comfort in order to conform to an adaptable frame design. Whether you'll want to modify?? Up your eyes to adapt to your sunglasses, which may be produced by the identical company, it may guarantee exactly the same service quality, mixing style and practicality. Are looking at the key items for your sunglasses, polarized contacts is found throughout the sunglasses warehouse supplies, even if you're not a fashionable pursuit.

    Reply
  • Smart Device to WCF Router connection not working

    Posted by Leela Kumar on 03/11/2013 09:43pm

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

    Reply
  • Very nice..

    Posted by Malte Baden Hansen on 12/19/2012 12: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.

    Reply
  • Need more information

    Posted by SUJITH on 06/07/2012 10:38am

    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

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

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds