Compartilhar via


WCF 4.0 – Routing

One of the great new features of WCF is the Routing Service. Here is a little post to show you how simple this is to put in place a WCF Routing.

Sample

Code Snippet

  1. <?xml version="1.0"?>
  2. <configuration>
  3.   <system.web>
  4.     <compilation debug="true" targetFramework="4.0"/>
  5.   </system.web>
  6.   <system.serviceModel>
  7.     <behaviors>
  8.       <serviceBehaviors>
  9.         <behavior>
  10.           <serviceMetadata httpGetEnabled="true"/>
  11.           <serviceDebug includeExceptionDetailInFaults="false"/>
  12.         </behavior>
  13.         <behavior name="RoutingBehavior">
  14.           <routing routeOnHeadersOnly="false" filterTableName="filterTable1" />
  15.           <serviceDebug includeExceptionDetailInFaults="true"/>
  16.           <serviceMetadata httpGetEnabled="true" />
  17.         </behavior>
  18.       </serviceBehaviors>
  19.     </behaviors>
  20.     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  21.     <services>
  22.       <service behaviorConfiguration="RoutingBehavior" name="System.ServiceModel.Routing.RoutingService">
  23.           <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="RouterEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
  24.       </service>
  25.     </services>
  26.     <routing>
  27.       <filters>
  28.         <filter name="ServiceTestFilter1" filterType="EndpointAddress" filterData="https://localhost/MyRouting/RouterService.svc/Test" />
  29.       </filters>
  30.       <filterTables>
  31.         <filterTable name="filterTable1">
  32.           <add filterName="ServiceTestFilter1" endpointName="BasicHttpBinding_IService1" priority="0" />
  33.         </filterTable>
  34.       </filterTables>
  35.     </routing>
  36.  
  37.     <client>
  38.       <endpoint address="https://localhost/MyApplication/Service1.svc" binding="basicHttpBinding" bindingConfiguration="" contract="*" name="BasicHttpBinding_IService1" />
  39.     </client>
  40.  
  41.   </system.serviceModel>
  42.   <system.webServer>
  43.     <modules runAllManagedModulesForAllRequests="true"/>
  44.   </system.webServer>
  45. </configuration>

Creation of a svc for filters

First of all, create a svc file, named RouterService.svc, that implements the RoutingService.

Code Snippet

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

Defining your client EndPoint

First of all, in your web.config application, the final endpoints have to be configured like in common WCF scenarios.

Code Snippet

  1. <client>
  2.   <endpoint address="https://localhost/MyApplication/Service1.svc" binding="basicHttpBinding" bindingConfiguration="" contract="*" name="BasicHttpBinding_IService1" />
  3. </client>

Defining youe server EndPoint

Code Snippet

  1.   <behaviors>
  2.     <serviceBehaviors>
  3.       <behavior>
  4.         <serviceMetadata httpGetEnabled="true"/>
  5.         <serviceDebug includeExceptionDetailInFaults="false"/>
  6.       </behavior>
  7.       <behavior name="RoutingBehavior">
  8.         <routing routeOnHeadersOnly="false" filterTableName="filterTable1" />
  9.         <serviceDebug includeExceptionDetailInFaults="true"/>
  10.         <serviceMetadata httpGetEnabled="true" />
  11.       </behavior>
  12.     </serviceBehaviors>
  13.   </behaviors>
  14.   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  15.   <services>
  16.     <service behaviorConfiguration="RoutingBehavior" name="System.ServiceModel.Routing.RoutingService">
  17.         <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="RouterEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
  18.     </service>
  19.   </services>

Defining your filters and table filters

Code Snippet

  1. <routing>
  2.   <filters>
  3.     <filter name="ServiceTestFilter1" filterType="EndpointAddress" filterData="https://localhost/MyRouting/RouterService.svc/Test" />
  4.   </filters>
  5.   <filterTables>
  6.     <filterTable name="filterTable1">
  7.       <add filterName="ServiceTestFilter1" endpointName="BasicHttpBinding_IService1" priority="0" />
  8.     </filterTable>
  9.   </filterTables>
  10. </routing>

In <system.serviceModel>, create a <routing> tag, it contains:

  • <Filters>:  list of filters that will be applied. Name, filterType and filterData are required;
  • <FilterTables>: mapping of filters with EndPoints