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

1 comment: