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
- <?xml version="1.0"?>
- <configuration>
- <system.web>
- <compilation debug="true" targetFramework="4.0"/>
- </system.web>
- <system.serviceModel>
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- <behavior name="RoutingBehavior">
- <routing routeOnHeadersOnly="false" filterTableName="filterTable1" />
- <serviceDebug includeExceptionDetailInFaults="true"/>
- <serviceMetadata httpGetEnabled="true" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
- <services>
- <service behaviorConfiguration="RoutingBehavior" name="System.ServiceModel.Routing.RoutingService">
- <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="RouterEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
- </service>
- </services>
- <routing>
- <filters>
- <filter name="ServiceTestFilter1" filterType="EndpointAddress" filterData="https://localhost/MyRouting/RouterService.svc/Test" />
- </filters>
- <filterTables>
- <filterTable name="filterTable1">
- <add filterName="ServiceTestFilter1" endpointName="BasicHttpBinding_IService1" priority="0" />
- </filterTable>
- </filterTables>
- </routing>
- <client>
- <endpoint address="https://localhost/MyApplication/Service1.svc" binding="basicHttpBinding" bindingConfiguration="" contract="*" name="BasicHttpBinding_IService1" />
- </client>
- </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
Creation of a svc for filters
First of all, create a svc file, named RouterService.svc, that implements the RoutingService.
Code Snippet
- <%@ ServiceHost Language="C#" Debug="true"
- 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
- <client>
- <endpoint address="https://localhost/MyApplication/Service1.svc" binding="basicHttpBinding" bindingConfiguration="" contract="*" name="BasicHttpBinding_IService1" />
- </client>
Defining youe server EndPoint
Code Snippet
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- <behavior name="RoutingBehavior">
- <routing routeOnHeadersOnly="false" filterTableName="filterTable1" />
- <serviceDebug includeExceptionDetailInFaults="true"/>
- <serviceMetadata httpGetEnabled="true" />
- </behavior>
- </serviceBehaviors>
- </behaviors>
- <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
- <services>
- <service behaviorConfiguration="RoutingBehavior" name="System.ServiceModel.Routing.RoutingService">
- <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="RouterEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter"/>
- </service>
- </services>
Defining your filters and table filters
Code Snippet
- <routing>
- <filters>
- <filter name="ServiceTestFilter1" filterType="EndpointAddress" filterData="https://localhost/MyRouting/RouterService.svc/Test" />
- </filters>
- <filterTables>
- <filterTable name="filterTable1">
- <add filterName="ServiceTestFilter1" endpointName="BasicHttpBinding_IService1" priority="0" />
- </filterTable>
- </filterTables>
- </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