This project is read-only.

RouteTable vs WebApiRoute vs WebServiceHost2Factory...which one to use & why?

Topics: Web Api
Jan 9, 2012 at 8:59 PM

 I trying to develop WCF RESTful service. I’m using VS 2010 Premium with .Net 4. I have downloaded WCF REST Starter Kit and also downloaded WCF REST templates for VS 2010 from visual studio gallery. (WCF REST Service Template 40(CS) ). I have to install the templates from gallary becuase the templates that comes with WCF REST Starter Kit only works with VS 2008

On all the learning videos & MSDN it says use WebServiceHost2 & WebServiceHost2Factory class for RESTful services, because these classes are specifically designed for REST services and they are included in Microsoft.ServiceModel.Web.dll. Also the examples I have seen so far uses .svc file to host the service.

So I created a project using the WCF REST Template which I have downloaded from Visual Studio gallery. But it created a service class without using .svc file and also there is no reference to Microsoft.ServiceModel.Web.dll. After little bit of digging and goggling I found its using RouteTable class . Below is the code in global.ascx added by the template

RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));

If you note, its not using WebServiceHost2Factory instead its using WebServiceHostFactory.

These is also new WCF Web API which says "ServiceRoute has a known issue with that causes it to interfere with ASP.NET MVC action link generation. To work around this issue use WebApiRoute in WCF Web API Enhancements instead."

So now I’m completely confused which is the best way to develop & start WCF RESTful service and what’s the advantage of one over other. I know these approaches will work but which one is preferred approach

Again, im using VS 2010, .Net 4, IIS 7.5

Jan 9, 2012 at 10:38 PM

You should use WCF Web API instead of the WCF REST Starter Kit. You can quickly get it by installing NuGet and then installing the WebApi.All package into your project.

Daniel Roth

Jan 9, 2012 at 11:01 PM

but why should i use WCF Web API, i mean whats the adavantage. Is WebServiceHost2 and WebServiceHost2Factory obsolete now? or is the funtionality of these classes now available in WCF Web API? What about the interceptors, are those available in WCF Web Api for user authentication?

Jan 9, 2012 at 11:08 PM

WCF REST Starter Kit became WCF WebHttp in .NET 4 and WCF Web API is the next turn of the crank after that. The WCF REST Starter Kit is not supported at this point. WCF Web API adds full content negotiation support, a rich integrated test client, code-based config, operation handlers, etc. It’s still in preview form, and it is getting integrated with MVC 4, but it represents the latest effort in this space.

Daniel Roth

Jan 10, 2012 at 3:50 PM