Sunday, April 25, 2010

WCF 4.0 : File less activation and Custom service host

With WCF 4.0 comes a feature where in a developer will not required to have a .svc file physically. An svc file is required for the IIS or WAS so that the servicehost can instantiate a new service using IInstanceprovider and as specfied in svc markup.This can be circumvented in .NET 3.5 by writing a UrlReWrite module in IIS. But that gives you a new set of maintainability problems. This .svc file can be a bit painful in REST based services as it violates REST principles.But in .NET 4.0, dependency on ,.svc has been addressed and look how easy it is. Whats good is this can coupled with a custom service host. Lets look at the implemenation

I have a WCF service project configured as a virtual directory. My service class is "FileLessActivationTest1.Service1" where "FileLessActivationTest1" in the namespace. Open the web.config and enter the following.

Basically it says that if there is a request made for Service1.svc, use Service1 class. Basically that's what svc file did previously. Now you can delete .svc file and check out. Remember, service1.cs is still required.
Hold on, that wasnt the only thing .svc file achieved. It also had the option to specify a service host.
Lets define our custom service host as follows

 Now amend the service activation in the config as shown below.

Just like in .svc file, this has an attribute called factory and all we have to do is to supply the factory class name. Thats it. We have a WCF service with file less activation and with custom service host.

The sample is here

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