WCF4 has great new routing capabilities that save you from writing your own message-routing solution. A routing solution can be very useful for many scenarios, such as
Crossing network boundaries
Redundancy—providing alternative endpoints in case of failure
Load balancing
Bridging of different protocols
Versioning
Providing an additional layer of security
WCF4's routing capabilities support all of these scenarios and allow you to listen for incoming WCF communications and route them, depending on customizable criteria. Let's create a simple routing example now to route messages from one endpoint to another.
We will create a very simple routing service that will listen for all calls to the endpoint http://localhost:1000/Router and route them through to a service at http://localhost:1111/TestService.
Open Visual Studio and create a new console application called Chapter7.Router.
Add a WCF service library project called Chapter7.RouterTestService to the solution.
Add a project reference in Chapter7.Router to Chapter7Router.TestService.
In Chapter7.Router, add a reference to the following assemblies: System.ServiceModel and System.ServiceModel.Routing.
In Chapter7.Router, open Program.cs and replace the existing code with the following:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.ServiceModel.Routing; namespace Chapter7.Router { class Program { static void Main(string[] args) {
//Open client service ServiceHost ClientService = new ServiceHost(typeof(Chapter7.RouterTestService.Service1), new Uri("http://localhost:1111/TestService")); ClientService.Open(); Console.WriteLine("Service running..."); //Open routing service ServiceHost RouterService = new ServiceHost(typeof(RoutingService)); RouterService.Open(); Console.WriteLine("Routing service running"); Console.ReadLine(); ClientService.Close(); RouterService.Close(); } } }
We now need to define our routing rules. Add an App.config file to the Chapter7.Router project and enter the following configuration:
<configuration> <system.serviceModel> <services> <service behaviorConfiguration="routingData" name="System.ServiceModel.Routing.RoutingService"> <host> <baseAddresses> <add baseAddress="http://localhost:1000/Router"/> </baseAddresses> </host> <endpoint address="" binding="basicHttpBinding" name="requestReplyEndpoint" contract="System.ServiceModel.Routing.IRequestReplyRouter" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="routingData"> <serviceMetadata httpGetEnabled="True"/> <routing filterTableName="MyRoutingTable" /> </behavior> </serviceBehaviors> </behaviors> <client> <endpoint name="ServiceInstance" address="http://localhost:1111/TestService" binding="basicHttpBinding" contract="*"> </endpoint> </client>
<routing> <filters> <filter name="MatchAllFilter" filterType="MatchAll" /> </filters> <filterTables> <filterTable name="MyRoutingTable"> <add filterName="MatchAllFilter" endpointName="ServiceInstance" /> </filterTable> </filterTables> </routing> </system.serviceModel> </configuration>
Now open a browser and go to http://localhost:1000/Router. If everything is working properly, then you should find that your request is routed through to the service at http://localhost:1111/TestService.
Note how configuring the router was very similar to configuring any other service. Routing services support any endpoint that WCF does.
In the la7st example, we created a simple filter that would route any type of communication. Of course, normally you will want to route messages depending on specific conditions. WCF provides a number of options for defining more complex filters, including
XPathMessageFilter (XPath queries against incoming messages)
ActionMessageFilter (WS-Addressing "action" parameters)
EndpointAddressMessageFilter and PrefixEndpointAddressMessageFilter (match against endpoint address)
Your own filters
This example shows the creation of an ActionMessage filter that would be added to the entries section:
<filter name="addFilter" filterType="Action" filterData="http://www.apress.com/Book"/>
You can use the new routing functionality to multicast messages by creating filters that will be matched multiple times with different endpoints. For example, you could route messages to three different endpoints using the following configuration:
<add filterName="MatchAllFilter" endpointName="ServiceInstance1" /> <add filterName="MatchAllFilter" endpointName="ServiceInstance2" /> <add filterName="MatchAllFilter" endpointName="ServiceInstance3" />
The router service can also be used to bridge between the bindings that are used. For example, on an internal trusted network, you could use an unsecured connection for better performance, and then bridge it to a secure connection for external communication.
You can also use the new routing functionality to define a list of backup endpoints that will be used if WCF encounters a CommunicationException or TimeoutException on the main endpoints. To define a list of backup endpoints, create a new backupLists section inside the routing block like so:
<backupLists> <backupList name="backupList"> <add endpointName="fallover1" /> <add endpointName="fallover2" /> </backupList> </backupLists>
3.131.38.104