Lets say you have 2 service -
Service1.svc and Service2.svc both having the same service contract. Based on
the request, you would route it to Service1 or Service2.
For this scenario, WCF has
the feature of Routing Service. You create a third service (say Service.svc ) which will not have any
code but some configuration to parse the request XML by Xpath and sending the
request to the desired service.
Below is a sample config for
a Routing service which routes based on the value of applicationType which is a parameter in the Service contract. –
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior
name="RoutingBehavior">
<serviceMetadata
httpGetEnabled="True"/>
<routing
routeOnHeadersOnly="false"
filterTableName="ApplicationRoutingTable" />
<serviceDebug
includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<routing>
<namespaceTable>
<add
namespace="http://mikedemo/contracts" prefix="o"/>
</namespaceTable>
<filters>
<filter
name="XYZApplicationFilter" filterType="XPath"
filterData="//o:applicationType = 'XYZ'"/>
<filter
name="NonXYZApplicationFilter" filterType="XPath"
filterData="//o:applicationType != 'XYZ'"/>
</filters>
<filterTables>
<filterTable
name="ApplicationRoutingTable">
<add
filterName="XYZApplicationFilter"
endpointName="XYZApplication" priority="0" />
<add
filterName="NonXYZApplicationFilter"
endpointName="NonXYZApplication" priority="0" />
</filterTable>
</filterTables>
</routing>
<services>
<service
behaviorConfiguration="RoutingBehavior"
name="System.ServiceModel.Routing.RoutingService">
<endpoint binding="basicHttpBinding"
bindingConfiguration=""
name="RoutingEndpoint"
contract="System.ServiceModel.Routing.IRequestReplyRouter" />
<endpoint address="mex"
binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<client>
<endpoint
name="XYZApplication" contract="*"
binding="basicHttpBinding"
address="http://localhost/WcfServiceA/Service2.svc" />
<endpoint
name="NonXYZApplication" contract="*"
binding="basicHttpBinding" address="http://localhost/WcfServiceA/Service1.svc"
/>
</client>
</system.serviceModel>
When you want to use the “Routing
Service”, just add a reference to any of Service1 or Service2 to generate the
metadata. At the end, just modify the endpoint in web.config to point to the
Routing Service instead of Service1 or Service2.
No comments:
Post a Comment