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.
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 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 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
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=184.108.40.206, 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.
About the Author
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.
Top White Papers and Webcasts
As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...
Most Popular Programming Stories
- There have been no articles posted today.