Sunday, April 18, 2010

WCF 4.0 : Routing Service(Content Based routing)

WCF 4.0 comes with whole lot of new features primarily aimed at enhancing Rapid application development.Features like svc less WCF service,enhanced and simplified config makes it even more productive to develop and deploy WCF services. One such feature is the WCF 4.0 routing service.

Lets assume a scenario where we have a customer processing service. We have one service that processes legacy customers(customers that are already part of business process). And another service that processes entirely new customers. In .NET 3.5, a WCF client would have to call the appropriate service based on the customer type. So the clients needs to aware of two endpoints. Teh service owner needs to apply security,configuration,transaction supports to each service individually.

To relieve the WCF clients pain of deciding the service to be called, we can have a WCF intermediary service whose endpoint is exposed to the client and the remaining services endpoints are exposed only to this intermediary service.But this requires the service developer to write an intermediary service which takes a lot of plumbing and therefore a lot of effort. Check this article by Michele Leroux Bustamante. WCF Routing .

But thats in .NET 3.5. Come WCF 4.0 and looks what feature has been added. We have something called as WCF routing service which takes care of the rotuing. All you need to code is the logic to route.

Our solution has five projects as shown below.One class library to hold the re usable contracts. Two services(new customer service and legacy customer) that use the contracts in the contract librayr project. One routin service and the console client. The WCF services have following service implementation



public string ProcessCustomer(CustomerInformation info)

{
return "Legacy customer processed";
}

Nothing fancy in there. Now lets turn our attention to the routing service.Remove all the auto generated stuff except for the svc file and the config. Even the code behind can deleted. In teh svc make sure your mark up is like this


And now to the web.config. Add the actual endpoint addresses of our legacy and new customer service.

<client>
<endpoint address="http://localhost:62662/NewCustomer.svc" binding="basicHttpBinding" contract="*" name="NewCustomerEP"></endpoint>
<endpoint address="http://localhost:62656/LegacyCustomer.svc" binding="basicHttpBinding" contract="*" name="LegacyCustomerEP"></endpoint>
</client>

Next we have to inform the routing logic and set up the routing filters as below.

<routing>
<namespaceTable>
<add namespace="http://schemas.datacontract.org/2004/07/ContractLibrary" prefix="custom"/>
</namespaceTable>
<filterTables>
<filterTable name="filterTable1">
<add filterName="LegacyCustomerFilter" priority="0" endpointName="LegacyCustomerEP" />
<add filterName="NewCustomerFilter" priority="0" endpointName="NewCustomerEP" />
</filterTable>
</filterTables>
<filters>
<filter name="LegacyCustomerFilter" filterType="XPath" filterData="//custom:CustomerType='LEGACY'"/>
<filter name="NewCustomerFilter" filterType="XPath" filterData="//custom:CustomerType='NEW'"/>
</filters>
</routing>
The above filter setup says that if CustomerType element in the message equals 'LEGACY' then route it to legacy service endpoint and if its 'NEW' then route it to new service endpoint.This is simplified using XPATH which is just one of the otions to filter. Its that simple!!But yes a bit of work might be needed to identify the XPATH query.

The Source Code is here

No comments:

Post a Comment